﻿using BrnMall.Core;
using BrnMall.Manager.Manager.Product;
using BrnMall.Services;
using QIDong.WeApp.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Http;
using System.Web.ModelBinding;
using BrnMall.Manager.Manager.Store;
using WeiTap.Restful;
using BrnMall.Business;
using BrnMall.Manager.Domain.User;
using BrnMall.Manager.Manager.Cart;
using BrnMall.Manager.Manager.Restaurant;
using BrnMall.Manager.Manager.User;
using PetaPoco;
using BrnMall.Manager.Manager.WeApp;
using Logistics.KDNiao;

namespace BrnMall.Web.RestApi.Controllers
{
    /// <summary>
    /// 订单控制器类
    /// </summary>
    public class OrderController : WebApi
    {
        private static object _locker = new object();//锁对象
        private static object _directpaymentlocker = new object();//锁对象


        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="storeid">店铺ID</param>
        /// <param name="osn">订单编号</param>
        /// <param name="accountName">账户名</param>
        /// <param name="consignee">收货人</param>
        /// <param name="orderState">订单状态</param>
        /// <param name="sid">全局标示：餐位号</param>
        /// <param name="pageSize">每页数</param>
        /// <param name="pageNumber">当前页数</param>
        /// <returns></returns>
        /// <returns></returns>
        public OrderListModel OrderList(int storeid, string osn = "", string accountName = "", string consignee = "", int orderState = 0, string sid = "",
            int pageSize = 15, int pageNumber = 1)
        {
            //获取用户id
            int uid = Users.GetUidByAccountName(accountName);

            string condition = AdminOrders.GetOrderListCondition(storeid, osn, uid, consignee, orderState, sid);

            PageModel pageModel = new PageModel(pageSize, pageNumber, AdminOrders.GetOrderCount(condition));



            OrderListModel model = new OrderListModel()
            {
                OrderList = AdminOrders.GetOrderList(pageModel.PageSize, pageModel.PageNumber, condition),
                PageModel = pageModel,
                OSN = osn,
                AccountName = accountName,
                Consignee = consignee,
                OrderState = orderState,
                OrderStateList = new List<OrderStateModel>()
            };

            model.OrderStateList.Add(new OrderStateModel() { Name = "全部", Value = "0" });
            model.OrderStateList.Add(new OrderStateModel() { Name = "等待付款", Value = ((int)OrderState.WaitPaying).ToString() });
            model.OrderStateList.Add(new OrderStateModel() { Name = "待确认", Value = ((int)OrderState.Confirming).ToString() });
            model.OrderStateList.Add(new OrderStateModel() { Name = "已确认", Value = ((int)OrderState.Confirmed).ToString() });
            model.OrderStateList.Add(new OrderStateModel() { Name = "备货中", Value = ((int)OrderState.PreProducting).ToString() });
            model.OrderStateList.Add(new OrderStateModel() { Name = "已发货", Value = ((int)OrderState.Sended).ToString() });
            model.OrderStateList.Add(new OrderStateModel() { Name = "已收货", Value = ((int)OrderState.Received).ToString() });
            model.OrderStateList.Add(new OrderStateModel() { Name = "已锁定", Value = ((int)OrderState.Locked).ToString() });
            model.OrderStateList.Add(new OrderStateModel() { Name = "已取消", Value = ((int)OrderState.Cancelled).ToString() });
            return model;
        }

        /// <summary>
        /// 更新订单配送费用
        /// </summary>
        /// <param name="uid">更改人UID</param>
        /// <param name="oid">订单编号</param>
        /// <param name="shipFee">配送费</param>
        /// <returns></returns>
        public bool UpdateOrderShipFee(int uid, int oid, decimal shipFee)
        {
            if (shipFee < 0)
                throw new Exception("配送费用不能小于0");
            var orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null)
                throw new Exception("订单不存在");
            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                throw new Exception("不能修改订单配送费用");


            decimal change = shipFee - orderInfo.ShipFee;
            Orders.UpdateOrderShipFee(orderInfo.Oid, shipFee, orderInfo.OrderAmount + change, orderInfo.SurplusMoney + change);
            OrderActions.CreateOrderAction(new OrderActionInfo()
            {
                Oid = oid,
                Uid = uid,
                RealName = Users.GetUserDetailById(uid).RealName,
                ActionType = (int)OrderActionType.UpdateShipFee,
                ActionTime = DateTime.Now,
                ActionDes = "您订单的配送费用已经修改"
            });


            if ((orderInfo.SurplusMoney + change) <= 0)
                Orders.UpdateOrderState(oid, OrderState.Confirming);

            return true;

        }
        /// <summary>
        /// 更新订单折扣
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="oid">订单ID</param>
        /// <param name="discount">折扣</param>
        /// <returns></returns>
        public bool UpdateOrderDiscount(int uid, int oid, decimal discount)
        {
            if (discount < 0)
                throw new Exception("订单折扣不能小于0");
            OrderInfo orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null)
                throw new Exception("订单不存在");

            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                throw new Exception("不能修改订单折扣");

            if (discount > (orderInfo.SurplusMoney + orderInfo.Discount))
                throw new Exception("折扣不能大于需支付金额");

            decimal surplusMoney = orderInfo.SurplusMoney + orderInfo.Discount - discount;
            Orders.UpdateOrderDiscount(orderInfo.Oid, discount, surplusMoney);
            OrderActions.CreateOrderAction(new OrderActionInfo()
            {
                Oid = oid,
                Uid = uid,
                RealName = Users.GetUserDetailById(uid).RealName,
                ActionType = (int)OrderActionType.UpdateDiscount,
                ActionTime = DateTime.Now,
                ActionDes = "您订单的折扣已经修改"
            });

            if (surplusMoney <= 0)
                Orders.UpdateOrderState(oid, OrderState.Confirming);

            return true;

        }
        /// <summary>
        /// 备货
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="oid">订单ID</param>
        /// <returns></returns>
        public bool PreOrderProduct(int uid, int oid)
        {
            OrderInfo orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null)
                throw new Exception("订单不存在");

            if (orderInfo.OrderState != (int)OrderState.Confirmed)
                throw new Exception("订单还未确认，请先确认");

            Orders.PreProduct(orderInfo);
            OrderActions.CreateOrderAction(new OrderActionInfo()
            {
                Oid = oid,
                Uid = uid,
                RealName = Users.GetUserDetailById(uid).RealName,
                ActionType = (int)OrderActionType.PreProduct,
                ActionTime = DateTime.Now,
                ActionDes = "您的订单已经备货完成"
            });

            return true;
        }
        /// <summary>
        /// 发货
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="oid">订单ID</param>
        /// <param name="shipCoId">发货公司ID</param>
        /// <param name="shipSn">运单号</param>
        /// <returns></returns>
        public bool SendOrderProduct(int uid, int oid, int shipCoId, string shipSn)
        {
            if (string.IsNullOrEmpty(shipSn))
                throw new Exception("运单号不能为空");
            if (shipSn.Length > 30)
                throw new Exception("发货单号长度不能超过30");
            OrderInfo orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null)
                throw new Exception("订单不存在");
            if (orderInfo.OrderState != (int)OrderState.PreProducting)
                throw new Exception("订单还未完成备货,不能发货");
            ShipCompanyInfo shipCompanyInfo = ShipCompanies.GetShipCompanyById(shipCoId);
            if (shipCompanyInfo == null)
                throw new Exception("请选择配送公司");
            Orders.SendOrder(oid, OrderState.Sended, shipSn, shipCoId, shipCompanyInfo.Name, DateTime.Now);
            OrderActions.CreateOrderAction(new OrderActionInfo()
            {
                Oid = oid,
                Uid = uid,
                RealName = Users.GetUserDetailById(uid).RealName,
                ActionType = (int)OrderActionType.Send,
                ActionTime = DateTime.Now,
                ActionDes = "您的订单已经发货,发货方式为:" + shipCompanyInfo.Name + "，单号为：" + shipSn
            });
            var t = new Thread(() => Orders.SendDeliverSuccessMessage(oid));
            t.Start();
            return true;
        }
        /// <summary>
        /// 获取配送公司
        /// </summary>
        /// <returns></returns>
        public List<ShipCompanyInfo> GetShipCompanyList()
        {
            return ShipCompanies.GetShipCompanyList();
        }

        /// <summary>
        /// 锁定订单
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="oid">订单ID</param>
        /// <returns></returns>
        public bool LockOrder(int uid, int oid)
        {
            OrderInfo orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null)
                throw new Exception("订单不存在");

            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                throw new Exception("订单当前不能锁定");

            Orders.LockOrder(orderInfo);

            OrderActions.CreateOrderAction(new OrderActionInfo()
            {
                Oid = oid,
                Uid = uid,
                RealName = Users.GetPartUserById(uid).NickName,
                ActionType = (int)OrderActionType.Lock,
                ActionTime = DateTime.Now,
                ActionDes = "订单已锁定"
            });
            return true;
        }

        /// <summary>
        /// 订单确认，购物车采用小程序结算的方式
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public ConfirmOrderModel ConfirmOrder(ConfirmOrderInputDto data)
        {
            try
            {
                if (data.Products.Count < 1)
                    throw new Exception("购物车中没有商品，请先添加商品");
                var orderProductList = CastFromPartProductInfo(data.Products);
                var storeCarInfo = Carts.TidyStoreOrderProductList(null, orderProductList);
                var model = new ConfirmOrderModel { PayMode = 1 };
                if (data.SaId > 0)
                    model.DefaultFullShipAddressInfo = ShipAddresses.GetFullShipAddressBySAId(data.SaId, data.Uid);
                if (model.DefaultFullShipAddressInfo == null)
                    model.DefaultFullShipAddressInfo = ShipAddresses.GetDefaultFullShipAddress(data.Uid);
                var partUserInfo = Users.GetPartUserById(data.Uid);
                var creditConfig = StoreConfigManager.GetStoreCreditConfigInfo(partUserInfo.RegisterFromStoreId);
                model.PayCreditName = creditConfig?.PayCreditName ?? "";
                model.UserPayCredits = partUserInfo.PayCredits;
                model.PayCreditPrice = creditConfig?.PayCreditPrice ?? 0;
                model.MaxUsePayCredits = Credits.GetOrderMaxUsePayCredits(partUserInfo.RegisterFromStoreId, partUserInfo.PayCredits);
                model.AllProductAmount = Carts.SumOrderProductAmount(storeCarInfo.SelectedOrderProductList);
                model.AllFullCut = Carts.SumFullCut(storeCarInfo);
                model.AllShipFee = model.DefaultFullShipAddressInfo != null ? Orders.GetShipFee(model.DefaultFullShipAddressInfo.ProvinceId, model.DefaultFullShipAddressInfo.CityId, storeCarInfo.SelectedOrderProductList) : 0;
                model.AllTotalCount = Carts.SumOrderProductCount(storeCarInfo.SelectedOrderProductList);
                model.AllTotalWeight = Carts.SumOrderProductWeight(storeCarInfo.SelectedOrderProductList);
                model.AllOrderAmount = model.AllProductAmount - model.AllFullCut;
                if (model.AllOrderAmount < 0.01M) model.AllOrderAmount = 0.01M;
                model.AllOrderAmount = model.AllOrderAmount + model.AllShipFee;
                return model;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        /// <summary>
        /// 确认订单，购物车采用后台结算的方式
        /// </summary>
        ///  <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public ConfirmOrderModel ConfirmOrderV2(ConfirmBackEndOrderInputDto data)
        {
            try
            {
                //选中的购物车项键列表
                string selectedCartItemKeyList = data.SelList;
                //配送地址id
                int saId = data.SaId;
                //支付方式
                int payMode = 1;

                //订单商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(data.Uid);
                if (orderProductList.Count < 1)
                {
                    return null;
                }

                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selectedCartItemKeyList), orderProductList);
                if (Carts.SumMallCartOrderProductCount(storeCartList) < 1)
                    throw new Exception("请先选择购物车商品");

                ConfirmOrderModel model = new ConfirmOrderModel();

                model.SelectedCartItemKeyList = selectedCartItemKeyList;
                model.PayMode = payMode;

                if (saId > 0)
                    model.DefaultFullShipAddressInfo = ShipAddresses.GetFullShipAddressBySAId(saId, data.Uid);
                if (model.DefaultFullShipAddressInfo == null)
                    model.DefaultFullShipAddressInfo = ShipAddresses.GetDefaultFullShipAddress(data.Uid);
                var partUserInfo = Users.GetPartUserById(data.Uid);
                var creditConfig = StoreConfigManager.GetStoreCreditConfigInfo(partUserInfo.RegisterFromStoreId);
                model.PayCreditName = creditConfig?.PayCreditName ?? "";
                model.UserPayCredits = partUserInfo.PayCredits;
                model.MaxUsePayCredits = Credits.GetOrderMaxUsePayCredits(partUserInfo.RegisterFromStoreId, partUserInfo.PayCredits);
                model.PayCreditPrice = creditConfig?.PayCreditPrice ?? 0;

                //餐饮模板,计算达达配送费
                bool isDaDaShip = false;
                if (saId > 0 && Stores.IsSessionApplication(partUserInfo.StoreAppId))
                {
                    if (Ships.ShopExists(orderProductList[0].StoreId))
                    {
                        var shipModel = Ships.GetShipFee(orderProductList[0].StoreId, model.DefaultFullShipAddressInfo.CityName, model.DefaultFullShipAddressInfo.Address);
                        if (Ships.IsSuccess(shipModel))
                        {
                            model.AllShipFee = (int)shipModel.Result.Fee;    //达达运费
                            isDaDaShip = true;
                        }
                        else
                            Trace.TraceError($"Order.ConfirmOrderV2() 获取配送费失败:{shipModel.Msg}");
                    }
                }
                List<StoreOrder> storeOrderList = new List<StoreOrder>();
                foreach (StoreCartInfo item in storeCartList)
                {
                    StoreOrder storeOrder = new StoreOrder();
                    storeOrder.StoreCartInfo = item;
                    storeOrder.ProductAmount = Carts.SumOrderProductAmount(item.SelectedOrderProductList);
                    storeOrder.FullCut = Carts.SumFullCut(item);
                    if (isDaDaShip)
                    {
                        storeOrder.ShipFee = model.AllShipFee;  //达达配送，计算运费
                    }
                    else
                    {
                        storeOrder.ShipFee = model.DefaultFullShipAddressInfo != null ? Orders.GetShipFee(model.DefaultFullShipAddressInfo.ProvinceId, model.DefaultFullShipAddressInfo.CityId, item.SelectedOrderProductList) : 0;
                        model.AllShipFee += storeOrder.ShipFee;
                    }
                    storeOrder.TotalCount = Carts.SumOrderProductCount(item.SelectedOrderProductList);
                    storeOrder.TotalWeight = Carts.SumOrderProductWeight(item.SelectedOrderProductList);
                    storeOrderList.Add(storeOrder);

                    model.AllFullCut += storeOrder.FullCut;
                    model.AllProductAmount += storeOrder.ProductAmount;
                    model.AllTotalCount += storeOrder.TotalCount;
                    model.AllTotalWeight += storeOrder.TotalWeight;
                }
                model.StoreOrderList = storeOrderList;
                if (saId == 0) //自提,不计算运费
                    model.AllShipFee = 0;

                model.RankDiscount = UserRanks.GetUserRankById(partUserInfo.UserRid)?.RankDiscount ?? 0;
                model.RankDiscountMoney = decimal.Round(model.AllProductAmount * model.RankDiscount / 100, 2);
                model.AllOrderAmount = model.AllProductAmount - model.AllFullCut - model.RankDiscountMoney + model.AllShipFee;

                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public ConfirmOrderModel SessionConfirmOrder(ConfirmBackEndOrderInputDto data)
        {
            if (string.IsNullOrEmpty(data?.Sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //选中的购物车项键列表
                string selectedCartItemKeyList = data.SelList;
                //配送地址id
                int saId = data.SaId;
                //支付方式
                int payMode = 1;

                //订单商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(data.Sid);
                if (orderProductList.Count < 1)
                {
                    return null;
                }
                //确认购物车代表需要后厨打印
                CartManager.UpdateProductIsPrint(orderProductList.Select(t => t.RecordId).ToList());
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selectedCartItemKeyList), orderProductList);
                if (Carts.SumMallCartOrderProductCount(storeCartList) < 1)
                    throw new Exception("请先选择购物车商品");

                ConfirmOrderModel model = new ConfirmOrderModel();

                model.SelectedCartItemKeyList = selectedCartItemKeyList;
                model.PayMode = payMode;

                if (saId > 0)
                    model.DefaultFullShipAddressInfo = ShipAddresses.GetFullShipAddressBySAId(saId, data.Uid);
                if (model.DefaultFullShipAddressInfo == null)
                    model.DefaultFullShipAddressInfo = ShipAddresses.GetDefaultFullShipAddress(data.Uid);
                var partUserInfo = Users.GetPartUserById(data.Uid);
                var creditConfig = StoreConfigManager.GetStoreCreditConfigInfo(partUserInfo.RegisterFromStoreId);
                model.PayCreditName = creditConfig?.PayCreditName ?? "";
                model.UserPayCredits = partUserInfo.PayCredits;
                model.MaxUsePayCredits = Credits.GetOrderMaxUsePayCredits(partUserInfo.RegisterFromStoreId, partUserInfo.PayCredits);
                model.PayCreditPrice = creditConfig?.PayCreditPrice ?? 0;
                List<StoreOrder> storeOrderList = new List<StoreOrder>();
                foreach (StoreCartInfo item in storeCartList)
                {
                    StoreOrder storeOrder = new StoreOrder();
                    storeOrder.StoreCartInfo = item;
                    storeOrder.ProductAmount = Carts.SumOrderProductAmount(item.SelectedOrderProductList);
                    storeOrder.FullCut = Carts.SumFullCut(item);
                    storeOrder.ShipFee = model.DefaultFullShipAddressInfo != null ? Orders.GetShipFee(model.DefaultFullShipAddressInfo.ProvinceId, model.DefaultFullShipAddressInfo.CityId, item.SelectedOrderProductList) : 0;
                    storeOrder.TotalCount = Carts.SumOrderProductCount(item.SelectedOrderProductList);
                    storeOrder.TotalWeight = Carts.SumOrderProductWeight(item.SelectedOrderProductList);
                    storeOrderList.Add(storeOrder);

                    //model.AllShipFee += storeOrder.ShipFee;
                    model.AllShipFee += 0;//不计算邮费
                    model.AllFullCut += storeOrder.FullCut;
                    model.AllProductAmount += storeOrder.ProductAmount;
                    model.AllTotalCount += storeOrder.TotalCount;
                    model.AllTotalWeight += storeOrder.TotalWeight;
                }
                model.StoreOrderList = storeOrderList;
                int dtId = 0;
                if (int.TryParse(data.Sid, out dtId))
                {
                    model.Discount = DiningTableManager.GetCurrDiscount(dtId);
                }
                model.RankDiscount = UserRanks.GetUserRankById(partUserInfo.UserRid)?.RankDiscount ?? 0;
                model.RankDiscountMoney = decimal.Round(model.AllProductAmount * model.RankDiscount / 100, 2);
                model.AllOrderAmount = model.AllProductAmount - model.AllFullCut - model.RankDiscountMoney + model.AllShipFee - model.Discount;


                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        private List<OrderProductInfo> CastFromPartProductInfo(List<OrderDetail> details)
        {
            var productList =
                Products.GetPartProductList(string.Join(",", details.Select(t => t.Pid)));
            var orderProductList = new List<OrderProductInfo>();
            foreach (var info in productList)
            {
                var p = new OrderProductInfo
                {
                    Pid = info.Pid,
                    AddTime = DateTime.Now,
                    BrandId = info.BrandId
                };
                p.BuyCount = details.Find(x => x.Pid == p.Pid).Number;
                p.CateId = info.CateId;
                p.CostPrice = info.CostPrice;
                p.CouponTypeId = 0;//暂时没有启动优惠券
                p.DiscountPrice = info.ShopPrice;//暂时没有启动折扣
                p.ExtCode1 = 0;//暂时不启用单品促销
                p.ExtCode2 = 0;//暂时不启用买送促销
                p.ExtCode3 = 0;//暂时不启用赠品促销
                p.ExtCode4 = 0;//暂时不启用满赠促销
                p.ExtCode5 = 0;//暂时不启用满减促销
                p.IsReview = 0;
                p.MarketPrice = info.MarketPrice;
                p.Name = info.Name;
                p.Oid = 0;
                p.PSN = info.PSN;
                p.PayCredits = 0;
                p.Weight = info.Weight;
                p.RealCount = p.BuyCount;
                p.RecordId = 0;
                p.SendCount = p.BuyCount;
                p.ShopPrice = info.ShopPrice;
                p.ShowImg = info.ShowImg;
                p.Sid = Sessions.GenerateSid();
                p.StoreCid = info.StoreCid;
                p.StoreId = info.StoreId;
                p.StoreSTid = info.StoreSTid;
                p.Type = 0;
                orderProductList.Add(p);
            }
            return orderProductList;
        }
        /// <summary>
        /// 修改订单折扣金额
        /// </summary>
        /// <param name="uid">操作人</param>
        /// <param name="oid">订单编号</param>
        /// <param name="discount">折扣金额</param>
        /// <returns></returns>
        public bool ChangeOrderDiscount(int uid, int oid, decimal discount)
        {
            OrderInfo orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null)
                throw new Exception("订单不存在");

            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                throw new Exception("不能修改订单折扣");

            if (discount > (orderInfo.SurplusMoney + orderInfo.Discount))
                throw new Exception("折扣不能大于需支付金额");


            decimal surplusMoney = orderInfo.SurplusMoney + orderInfo.Discount - discount;
            Orders.UpdateOrderDiscount(orderInfo.Oid, discount, surplusMoney);
            //创建订单处理
            OrderActions.CreateOrderAction(new OrderActionInfo()
            {
                Oid = orderInfo.Oid,
                Uid = uid,
                RealName = Users.GetPartUserById(uid)?.NickName,
                ActionType = (int)OrderActionType.UpdateDiscount,
                ActionTime = DateTime.Now,
                ActionDes = "您订单的折扣已经修改"
            });

            if (surplusMoney <= 0)
                Orders.UpdateOrderState(oid, OrderState.Confirming);

            return true;

        }
        /// <summary>
        /// 提交订单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public string SubmitOrder(CreateOrderInputDto data)
        {
            try
            {
                lock (_locker)
                {
                    var payCreditCount = 0;//支付积分
                    var payMode = 1;//在线支付
                    var couponIdList = new string[] { };//客户已经激活的优惠劵
                    var fullCut = 0;//满减金额
                    //验证配送地址是否为空
                    var fullShipAddressInfo = ShipAddresses.GetFullShipAddressBySAId(data.SaId, data.Uid);
                    if (fullShipAddressInfo == null)
                        throw new Exception("请选择配送地址");
                    //购物车商品列表
                    var orderProductList = CastFromPartProductInfo(data.Products);
                    if (orderProductList.Count < 1)
                        throw new Exception("购物车中没有商品");
                    var cartInfo = Carts.TidyStoreOrderProductList(null, orderProductList);
                    var storeCartList = new List<StoreCartInfo> { cartInfo };
                    var partUserInfo = Users.GetPartUserById(data.Uid);
                    //被选中的店铺数量
                    int selectedStoreCount = Carts.SumSelectedStoreCount(storeCartList);
                    var creditConfig = StoreConfigManager.GetStoreCreditConfigInfo(partUserInfo.RegisterFromStoreId);
                    //验证支付积分
                    if (payCreditCount > 0)
                    {
                        if (payCreditCount > partUserInfo.PayCredits)
                            throw new Exception("你使用的" + (creditConfig?.PayCreditName ?? "") + "数超过你所拥有的" + partUserInfo.PayCredits + "数");
                        if (payCreditCount > (creditConfig?.OrderMaxUsePayCredits ?? 0) * selectedStoreCount)
                            throw new Exception("此笔订单最多使用" + (creditConfig?.OrderMaxUsePayCredits ?? 0) * selectedStoreCount + "个" + (creditConfig?.PayCreditName ?? ""));
                    }

                    //验证优惠劵
                    List<CouponInfo> couponList = new List<CouponInfo>();
                    if (couponIdList.Length > 0)
                    {
                        foreach (string couponId in couponIdList)
                        {
                            int tempCouponId = TypeHelper.StringToInt(couponId);
                            if (tempCouponId > 0)
                            {
                                CouponInfo couponInfo = Coupons.GetCouponByCouponId(tempCouponId);
                                if (couponInfo == null)
                                    throw new Exception("优惠劵不存在");
                                else
                                    couponList.Add(couponInfo);
                            }
                        }
                    }
                    foreach (CouponInfo couponInfo in couponList)
                    {
                        #region  验证

                        CouponTypeInfo couponTypeInfo = Coupons.GetCouponTypeById(couponInfo.CouponTypeId);
                        if (couponTypeInfo == null)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵类型不存在");
                        }
                        else if (couponTypeInfo.State == 0)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵类型已关闭");
                        }
                        else if (couponTypeInfo.UseExpireTime == 0 && couponTypeInfo.UseStartTime > DateTime.Now)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵还未到使用时间");
                        }
                        else if (couponTypeInfo.UseExpireTime == 0 && couponTypeInfo.UseEndTime <= DateTime.Now)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵已过期");
                        }
                        else if (couponTypeInfo.UseExpireTime > 0 && couponInfo.ActivateTime <= DateTime.Now.AddDays(-1 * couponTypeInfo.UseExpireTime))
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵已过期");
                        }
                        else if (couponTypeInfo.UserRankLower > partUserInfo.UserRid)
                        {
                            throw new Exception("你的用户等级太低，不能使用编号为" + couponInfo.CouponSN + "优惠劵");
                        }
                        else if (couponList.Count > 1 && couponTypeInfo.UseMode == 1)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵不能叠加使用");
                        }
                        else
                        {
                            StoreCartInfo storeCartInfo = storeCartList.Find(x => x.StoreInfo.StoreId == couponTypeInfo.StoreId);

                            if (storeCartInfo == null)
                            {
                                throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能购买对应店铺的商品");
                            }

                            if (couponTypeInfo.OrderAmountLower > Carts.SumOrderProductAmount(storeCartInfo.SelectedOrderProductList))
                            {
                                throw new Exception("订单金额太低，不能使用编号为" + couponInfo.CouponSN + "优惠劵");
                            }

                            if (couponTypeInfo.LimitStoreCid > 0)
                            {
                                foreach (OrderProductInfo orderProductInfo in storeCartInfo.SelectedOrderProductList)
                                {
                                    if (orderProductInfo.Type == 0 && orderProductInfo.StoreCid != couponTypeInfo.LimitStoreCid)
                                    {
                                        throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能在购买" + Stores.GetStoreClassByStoreIdAndStoreCid(couponTypeInfo.StoreId, couponTypeInfo.LimitStoreCid).Name + "类的商品时使用");
                                    }
                                }
                            }

                            if (couponTypeInfo.LimitProduct == 1)
                            {
                                List<OrderProductInfo> commonOrderProductList = Carts.GetCommonOrderProductList(storeCartInfo.SelectedOrderProductList);
                                if (!Coupons.IsSameCouponType(couponTypeInfo.CouponTypeId, Carts.GetPidList(commonOrderProductList)))
                                    throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能在购买指定商品时使用");
                            }
                        }

                        #endregion
                    }
                    string pidList = Carts.GetMallCartPidList(storeCartList);//商品id列表
                    List<ProductStockInfo> productStockList = Products.GetProductStockList(pidList);//商品库存列表
                    List<SinglePromotionInfo> singlePromotionList = new List<SinglePromotionInfo>();//单品促销活动列表

                    //循环店铺购物车列表，依次验证
                    foreach (StoreCartInfo storeCartInfo in storeCartList)
                    {
                        if (!storeCartInfo.IsSelected) continue;

                        #region 验证购物车商品

                        foreach (CartProductInfo cartProductInfo in storeCartInfo.CartProductList)
                        {
                            if (!cartProductInfo.IsSelected) continue;

                            OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                            //验证商品信息
                            PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                            if (partProductInfo == null)
                            {
                                throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                            }
                            if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                            {
                                throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                            }

                            //验证商品库存
                            ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                            if (productStockInfo.Number < orderProductInfo.RealCount)
                            {
                                throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                            }
                            else
                            {
                                productStockInfo.Number -= orderProductInfo.RealCount;
                            }

                            //验证买送促销活动
                            if (orderProductInfo.ExtCode2 > 0)
                            {
                                BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                if (buySendPromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                }
                                else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                }
                                else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                    orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                    Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                    if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                        }
                                        Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                    }
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                }
                            }

                            //验证单品促销活动
                            if (orderProductInfo.ExtCode1 > 0)
                            {
                                SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                if (singlePromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                }
                                if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                }
                                if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(partUserInfo.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.IsStock == 1)
                                {
                                    SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                    if (temp == null)
                                    {
                                        temp = singlePromotionInfo;
                                        singlePromotionList.Add(temp);
                                    }

                                    if (temp.Stock < orderProductInfo.RealCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else
                                    {
                                        temp.Stock -= orderProductInfo.RealCount;
                                    }
                                }
                            }

                            //验证赠品促销活动
                            if (orderProductInfo.ExtCode3 > 0)
                            {
                                GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                if (giftPromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                }
                                else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                }
                                else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                    Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                {
                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                    Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (cartProductInfo.GiftList.Count > 0)
                                {
                                    List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                    if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                    foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                    {
                                        OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                        Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                        newGiftOrderProductList.Add(giftOrderProduct);
                                    }

                                    //验证赠品信息是否改变
                                    for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                    {
                                        OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                        OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                        if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                            newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                            newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                            newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                            newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                            newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                            newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                            newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                            newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                            newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                            newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                            newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                            newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                    }

                                    //验证赠品库存
                                    foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                    {
                                        ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                        if (stockInfo.Number < gift.RealCount)
                                        {
                                            if (stockInfo.Number == 0)
                                            {
                                                Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                if (cartProductInfo.GiftList.Count == 1)
                                                {
                                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                }
                                            }
                                            else
                                            {
                                                gift.RealCount = stockInfo.Number;
                                                Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                            }
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            stockInfo.Number -= gift.RealCount;
                                        }
                                    }
                                }
                            }
                        }

                        #endregion

                        #region 验证购物车套装

                        foreach (CartSuitInfo cartSuitInfo in storeCartInfo.CartSuitList)
                        {
                            if (!cartSuitInfo.IsSelected) continue;

                            SuitPromotionInfo suitPromotionInfo = Promotions.GetSuitPromotionByPmIdAndTime(cartSuitInfo.PmId, DateTime.Now);
                            if (suitPromotionInfo == null)
                            {
                                throw new Exception("商品" + cartSuitInfo.CartProductList[0].OrderProductInfo.Name + "的套装已经停止,请删除此套装");
                            }
                            else if (suitPromotionInfo.UserRankLower > partUserInfo.UserRid)
                            {
                                throw new Exception("你的用户等级太低，无法购买套装" + suitPromotionInfo.Name + "请删除此套装");
                            }
                            else if (suitPromotionInfo.QuotaUpper > 0 && cartSuitInfo.BuyCount > suitPromotionInfo.QuotaUpper)
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "要求每单最多购买" + suitPromotionInfo.QuotaUpper + "个");
                            }
                            else if (suitPromotionInfo.OnlyOnce == 1 && Promotions.IsJoinSuitPromotion(partUserInfo.Uid, suitPromotionInfo.PmId))
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "要求每人最多购买1个");
                            }

                            List<OrderProductInfo> newSuitOrderProductList = new List<OrderProductInfo>();
                            foreach (ExtSuitProductInfo extSuitProductInfo in Promotions.GetExtSuitProductList(cartSuitInfo.PmId))
                            {
                                OrderProductInfo suitOrderProductInfo = Carts.BuildOrderProduct(extSuitProductInfo);
                                Carts.SetSuitOrderProduct(suitOrderProductInfo, cartSuitInfo.BuyCount, extSuitProductInfo.Number, extSuitProductInfo.Discount, suitPromotionInfo);
                                newSuitOrderProductList.Add(suitOrderProductInfo);
                            }
                            List<OrderProductInfo> oldSuitOrderProductList = new List<OrderProductInfo>();
                            foreach (CartProductInfo cartProductInfo in cartSuitInfo.CartProductList)
                            {
                                oldSuitOrderProductList.Add(cartProductInfo.OrderProductInfo);
                            }
                            if (newSuitOrderProductList.Count != oldSuitOrderProductList.Count)
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "已经改变，请删除此套装后重新下单");
                            }
                            else
                            {
                                for (int i = 0; i < newSuitOrderProductList.Count; i++)
                                {
                                    OrderProductInfo newSuitOrderProductInfo = newSuitOrderProductList[i];
                                    OrderProductInfo oldSuitOrderProductInfo = oldSuitOrderProductList[i];
                                    if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                        newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                        newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                        newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                        newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                        newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                        newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                        newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                        newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                        newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("套装" + suitPromotionInfo.Name + "已经改变，请删除此套装后重新下单");
                                    }
                                }

                                foreach (CartProductInfo cartProductInfo in cartSuitInfo.CartProductList)
                                {
                                    OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                    //验证商品信息
                                    PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                    if (partProductInfo == null)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "已经下架，请删除此套装");
                                    }
                                    if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "信息有变化，请删除套装后重新添加");
                                    }

                                    //验证商品库存
                                    ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                    if (productStockInfo.Number < orderProductInfo.RealCount)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "库存不足");
                                    }
                                    else
                                    {
                                        productStockInfo.Number -= orderProductInfo.RealCount;
                                    }

                                    //验证赠品促销活动
                                    if (orderProductInfo.ExtCode3 > 0)
                                    {
                                        GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                        if (giftPromotionInfo == null)
                                        {
                                            throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此套装后重新添加");
                                        }
                                        else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                        {
                                            throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                        }
                                        else if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                            if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                            {
                                                throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                            }
                                            List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                            foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                            {
                                                OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                                Carts.SetGiftOrderProduct(giftOrderProduct, 2, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                                newGiftOrderProductList.Add(giftOrderProduct);
                                            }

                                            //验证赠品信息是否改变
                                            for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                            {
                                                OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                                OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                                if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                    newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                    newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                    newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                    newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                    newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                    newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                    newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                    newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                    newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2)
                                                {
                                                    throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                                }
                                            }

                                            //验证赠品库存
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                                if (stockInfo.Number < gift.RealCount)
                                                {
                                                    if (stockInfo.Number == 0)
                                                    {
                                                        Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                        if (cartProductInfo.GiftList.Count == 1)
                                                        {
                                                            orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                            Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                        }
                                                    }
                                                    else
                                                    {
                                                        gift.RealCount = stockInfo.Number;
                                                        Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                    }
                                                    throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                                }
                                                else
                                                {
                                                    stockInfo.Number -= gift.RealCount;
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                        }

                        #endregion

                        #region 验证购物车满赠

                        foreach (CartFullSendInfo cartFullSendInfo in storeCartInfo.CartFullSendList)
                        {
                            if (cartFullSendInfo.IsEnough && cartFullSendInfo.FullSendMinorOrderProductInfo != null)
                            {
                                if (cartFullSendInfo.FullSendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    throw new Exception("你的用户等级太低，无法参加商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "的满赠促销活动,请先删除此赠品");
                                }
                                if (cartFullSendInfo.FullSendMinorOrderProductInfo.DiscountPrice != cartFullSendInfo.FullSendPromotionInfo.AddMoney)
                                {
                                    throw new Exception("商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "的满赠促销活动金额错误,请删除后重新添加");
                                }
                                if (!Promotions.IsExistFullSendProduct(cartFullSendInfo.FullSendPromotionInfo.PmId, cartFullSendInfo.FullSendMinorOrderProductInfo.Pid, 1))
                                {
                                    throw new Exception("商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "不是满赠赠品,请删除后再结账");
                                }

                                ProductStockInfo productStockInfo = Products.GetProductStock(cartFullSendInfo.FullSendMinorOrderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < cartFullSendInfo.FullSendMinorOrderProductInfo.RealCount)
                                {
                                    throw new Exception("满赠赠品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "库存不足,请删除此满赠赠品");
                                }
                                else
                                {
                                    productStockInfo.Number -= cartFullSendInfo.FullSendMinorOrderProductInfo.RealCount;
                                }
                            }

                            foreach (CartProductInfo cartProductInfo in cartFullSendInfo.FullSendMainCartProductList)
                            {
                                #region 验证

                                if (cartProductInfo.IsSelected) continue;

                                OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                //验证商品信息
                                PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                if (partProductInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                                }
                                if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                                }

                                //验证商品库存
                                ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < orderProductInfo.RealCount)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                                }
                                else
                                {
                                    productStockInfo.Number -= orderProductInfo.RealCount;
                                }

                                //验证买送促销活动
                                if (orderProductInfo.ExtCode2 > 0)
                                {
                                    BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                    if (buySendPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                        orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                        Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                        if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                            }
                                            Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                        }
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                    }
                                }

                                //验证单品促销活动
                                if (orderProductInfo.ExtCode1 > 0)
                                {
                                    SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (singlePromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(partUserInfo.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.IsStock == 1)
                                    {
                                        SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                        if (temp == null)
                                        {
                                            temp = singlePromotionInfo;
                                            singlePromotionList.Add(temp);
                                        }

                                        if (temp.Stock < orderProductInfo.RealCount)
                                        {
                                            orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                            orderProductInfo.PayCredits = 0;
                                            orderProductInfo.CouponTypeId = 0;
                                            orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                            Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                            throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            temp.Stock -= orderProductInfo.RealCount;
                                        }
                                    }
                                }

                                //验证赠品促销活动
                                if (orderProductInfo.ExtCode3 > 0)
                                {
                                    GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (giftPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                        if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                        List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                        foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                        {
                                            OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                            Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                            newGiftOrderProductList.Add(giftOrderProduct);
                                        }

                                        //验证赠品信息是否改变
                                        for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                        {
                                            OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                            OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                            if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                                newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                                newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                                newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                            {
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                            }
                                        }

                                        //验证赠品库存
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                            if (stockInfo.Number < gift.RealCount)
                                            {
                                                if (stockInfo.Number == 0)
                                                {
                                                    Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                    if (cartProductInfo.GiftList.Count == 1)
                                                    {
                                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                    }
                                                }
                                                else
                                                {
                                                    gift.RealCount = stockInfo.Number;
                                                    Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                }
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                            }
                                            else
                                            {
                                                stockInfo.Number -= gift.RealCount;
                                            }
                                        }
                                    }
                                }

                                #endregion
                            }
                        }

                        #endregion

                        #region 验证购物车满减

                        foreach (CartFullCutInfo cartFullCutInfo in storeCartInfo.CartFullCutList)
                        {
                            if (cartFullCutInfo.FullCutPromotionInfo.UserRankLower > partUserInfo.UserRid)
                            {
                                List<OrderProductInfo> updateFullCutOrderProductList = new List<OrderProductInfo>();
                                foreach (CartProductInfo cartProductInfo in cartFullCutInfo.FullCutCartProductList)
                                {
                                    cartProductInfo.OrderProductInfo.ExtCode5 = -1 * cartProductInfo.OrderProductInfo.ExtCode5;
                                    updateFullCutOrderProductList.Add(cartProductInfo.OrderProductInfo);
                                }
                                Carts.UpdateOrderProductFullCut(updateFullCutOrderProductList);
                                throw new Exception("你的用户等级太低，无法参加" + cartFullCutInfo.FullCutPromotionInfo.Name + "满减促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                            }

                            foreach (CartProductInfo cartProductInfo in cartFullCutInfo.FullCutCartProductList)
                            {
                                #region 验证

                                if (cartProductInfo.IsSelected) continue;

                                OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                //验证商品信息
                                PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                if (partProductInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                                }
                                if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                                }

                                //验证商品库存
                                ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < orderProductInfo.RealCount)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                                }
                                else
                                {
                                    productStockInfo.Number -= orderProductInfo.RealCount;
                                }

                                //验证买送促销活动
                                if (orderProductInfo.ExtCode2 > 0)
                                {
                                    BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                    if (buySendPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                        orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                        Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                        if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                            }
                                            Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                        }
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                    }
                                }

                                //验证单品促销活动
                                if (orderProductInfo.ExtCode1 > 0)
                                {
                                    SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (singlePromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(partUserInfo.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.IsStock == 1)
                                    {
                                        SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                        if (temp == null)
                                        {
                                            temp = singlePromotionInfo;
                                            singlePromotionList.Add(temp);
                                        }

                                        if (temp.Stock < orderProductInfo.RealCount)
                                        {
                                            orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                            orderProductInfo.PayCredits = 0;
                                            orderProductInfo.CouponTypeId = 0;
                                            orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                            Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                            throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            temp.Stock -= orderProductInfo.RealCount;
                                        }
                                    }
                                }

                                //验证赠品促销活动
                                if (orderProductInfo.ExtCode3 > 0)
                                {
                                    GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (giftPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                        if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                        List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                        foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                        {
                                            OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                            Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                            newGiftOrderProductList.Add(giftOrderProduct);
                                        }

                                        //验证赠品信息是否改变
                                        for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                        {
                                            OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                            OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                            if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                                newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                                newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                                newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                            {
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                            }
                                        }

                                        //验证赠品库存
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                            if (stockInfo.Number < gift.RealCount)
                                            {
                                                if (stockInfo.Number == 0)
                                                {
                                                    Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                    if (cartProductInfo.GiftList.Count == 1)
                                                    {
                                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                    }
                                                }
                                                else
                                                {
                                                    gift.RealCount = stockInfo.Number;
                                                    Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                }
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                            }
                                            else
                                            {
                                                stockInfo.Number -= gift.RealCount;
                                            }
                                        }
                                    }
                                }

                                #endregion
                            }
                        }

                        #endregion
                    }
                    if (Carts.SumMallCartFullCut(storeCartList) != fullCut)
                        throw new Exception("满减金额不正确,请刷新页面重新提交");
                    //验证已经通过,进行订单保存
                    int pCount = 0;
                    string oidList = "";
                    foreach (StoreCartInfo storeCartInfo in storeCartList)
                    {
                        List<SinglePromotionInfo> storeSinglePromotionList = singlePromotionList.FindAll(x => x.StoreId == storeCartInfo.StoreInfo.StoreId);
                        List<CouponInfo> storeCouponList = couponList.FindAll(x => x.StoreId == storeCartInfo.StoreInfo.StoreId);
                        int storeFullCut = Carts.SumFullCut(storeCartInfo);
                        Carts.IsPersistOrderProduct = false;//没有通过系统购物车购买

                        OrderInfo orderInfo = Orders.CreateOrder(partUserInfo, storeCartInfo.StoreInfo, storeCartInfo.SelectedOrderProductList, storeSinglePromotionList, fullShipAddressInfo, payMode == 0 ? 0 : 1, ref payCreditCount, storeCouponList, storeFullCut, "", new DateTime(1970, 1, 1), WebHelper.GetIP());
                        if (orderInfo != null)
                        {
                            oidList += orderInfo.Oid + ",";
                            //删除剩余的满赠赠品
                            if (storeCartInfo.RemainedOrderProductList.Count > 0)
                            {
                                List<OrderProductInfo> delOrderProductList = Carts.GetFullSendMinorOrderProductList(storeCartInfo.RemainedOrderProductList);
                                if (delOrderProductList.Count > 0)
                                {
                                    Carts.DeleteOrderProductList(delOrderProductList);
                                    pCount += Carts.SumOrderProductCount(storeCartInfo.RemainedOrderProductList) - delOrderProductList.Count;
                                }
                            }
                            //创建订单处理
                            OrderActions.CreateOrderAction(new OrderActionInfo()
                            {
                                Oid = orderInfo.Oid,
                                Uid = partUserInfo.Uid,
                                RealName = partUserInfo.NickName,
                                ActionType = (int)OrderActionType.Submit,
                                ActionTime = DateTime.Now,
                                ActionDes = orderInfo.OrderState == (int)OrderState.WaitPaying ? "您提交了订单，等待您付款" : "您提交了订单，请等待系统确认"
                            });
                        }
                        else
                        {
                            throw new Exception("提交失败，请联系管理员");
                        }
                    }
                    return oidList.TrimEnd(',');

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        /// <summary>
        /// 提交订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string SubmitOrderV2(CreateBackEndOrderInputDto data)
        {

            try
            {
                lock (_locker)
                {
                    if (!string.IsNullOrEmpty(data.Sid))
                        Carts.UpdateCartUidBySid(data.Uid, data.Sid);
                    string selectedCartItemKeyList = data.SelList;//选中的购物车项键列表
                    int saId = data.SaId;//配送地址id
                    int payMode = 1;//支付方式
                    int payCreditCount = data.PayCreditCount;//支付积分
                    string[] couponIdList = StringHelper.SplitString(data.CouponIdList ?? "");//客户已经激活的优惠劵
                    string[] couponSNList = StringHelper.SplitString(data.CouponSNList ?? "");//客户还未激活的优惠劵
                    int fullCut = data.FullCut;//满减金额
                    DateTime bestTime = new DateTime(1970, 1, 1);//最佳配送时间
                    string buyerRemark = data.BuyerRemark ?? "";//买家备注

                    var partUserInfo = Users.GetPartUserById(data.Uid);


                    //验证买家备注的内容长度
                    if (StringHelper.GetStringLength(buyerRemark) > 125)
                        throw new Exception("备注最多填写125个字");
                    FullShipAddressInfo fullShipAddressInfo;
                    if (saId == 0)
                    {
                        fullShipAddressInfo = new FullShipAddressInfo();
                    }
                    else
                    {
                        //验证配送地址是否为空
                        fullShipAddressInfo = ShipAddresses.GetFullShipAddressBySAId(saId, data.Uid);
                        if (fullShipAddressInfo == null)
                            throw new Exception("缺少收货地址");
                    }


                    //购物车商品列表
                    List<OrderProductInfo> orderProductList = Carts.GetCartProductList(data.Uid);
                    if (orderProductList.Count < 1)
                        throw new Exception("购物车中没有商品");

                    //店铺购物车列表
                    List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selectedCartItemKeyList), orderProductList);
                    if (Carts.SumMallCartOrderProductCount(storeCartList) < 1)
                        throw new Exception("购物车中没有选中的商品");

                    //被选中的店铺数量
                    int selectedStoreCount = Carts.SumSelectedStoreCount(storeCartList);
                    var creditConfig = StoreConfigManager.GetStoreCreditConfigInfo(partUserInfo.RegisterFromStoreId);
                    //验证支付积分
                    if (payCreditCount > 0)
                    {
                        if (payCreditCount > partUserInfo.PayCredits)
                            throw new Exception("你使用的" + (creditConfig?.PayCreditName ?? "") + "数超过你所拥有的" + partUserInfo.PayCredits + "数");
                        if (payCreditCount > (creditConfig?.OrderMaxUsePayCredits ?? 0) * selectedStoreCount)
                            throw new Exception("此笔订单最多使用" + (creditConfig?.OrderMaxUsePayCredits ?? 0) * selectedStoreCount + "个" + (creditConfig?.PayCreditName ?? ""));
                    }

                    //验证 余额支付
                    decimal payCash = data.PayCash;     //支付的余额
                    if (payCash > 0)
                    {
                        var userCash = Manager.Manager.User.UserCashManager.GetInfo(data.Uid);
                        decimal balance = CashOrders.DecodeCash(userCash.Cash);

                        if (userCash == null || payCash > balance)
                            throw new Exception($"余额支付不能超过{balance}元");
                    }
                    if (payCash < 0) payCash = 0;


                    //验证优惠劵
                    List<CouponInfo> couponList = new List<CouponInfo>();
                    if (couponIdList.Length > 0)
                    {
                        foreach (string couponId in couponIdList)
                        {
                            int tempCouponId = TypeHelper.StringToInt(couponId);
                            if (tempCouponId > 0)
                            {
                                CouponInfo couponInfo = Coupons.GetCouponByCouponId(tempCouponId);
                                if (couponInfo == null)
                                    throw new Exception("优惠劵不存在");
                                else
                                    couponList.Add(couponInfo);
                            }
                        }
                    }
                    if (couponSNList.Length > 0)
                    {
                        foreach (string couponSN in couponSNList)
                        {
                            if (!string.IsNullOrWhiteSpace(couponSN))
                            {
                                CouponInfo couponInfo = Coupons.GetCouponByCouponSN(couponSN);
                                if (couponInfo == null)
                                    throw new Exception("优惠劵" + couponSN + "不存在");
                                else
                                    couponList.Add(couponInfo);
                            }
                        }
                    }
                    foreach (CouponInfo couponInfo in couponList)
                    {
                        #region  验证

                        CouponTypeInfo couponTypeInfo = Coupons.GetCouponTypeById(couponInfo.CouponTypeId);
                        if (couponTypeInfo == null)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵类型不存在");
                        }
                        else if (couponTypeInfo.State == 0)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵类型已关闭");
                        }
                        else if (couponTypeInfo.UseExpireTime == 0 && couponTypeInfo.UseStartTime > DateTime.Now)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵还未到使用时间");
                        }
                        else if (couponTypeInfo.UseExpireTime == 0 && couponTypeInfo.UseEndTime <= DateTime.Now)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵已过期");
                        }
                        else if (couponTypeInfo.UseExpireTime > 0 && couponInfo.ActivateTime <= DateTime.Now.AddDays(-1 * couponTypeInfo.UseExpireTime))
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵已过期");
                        }
                        else if (couponTypeInfo.UserRankLower > partUserInfo.UserRid)
                        {
                            throw new Exception("你的用户等级太低，不能使用编号为" + couponInfo.CouponSN + "优惠劵");
                        }
                        else if (couponList.Count > 1 && couponTypeInfo.UseMode == 1)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵不能叠加使用");
                        }
                        else
                        {
                            StoreCartInfo storeCartInfo = storeCartList.Find(x => x.StoreInfo.StoreId == couponTypeInfo.StoreId);

                            if (storeCartInfo == null)
                            {
                                throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能购买对应店铺的商品");
                            }

                            if (couponTypeInfo.OrderAmountLower > Carts.SumOrderProductAmount(storeCartInfo.SelectedOrderProductList))
                            {
                                throw new Exception("订单金额太低，不能使用编号为" + couponInfo.CouponSN + "优惠劵");
                            }

                            if (couponTypeInfo.LimitStoreCid > 0)
                            {
                                foreach (OrderProductInfo orderProductInfo in storeCartInfo.SelectedOrderProductList)
                                {
                                    if (orderProductInfo.Type == 0 && orderProductInfo.StoreCid != couponTypeInfo.LimitStoreCid)
                                    {
                                        throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能在购买" + Stores.GetStoreClassByStoreIdAndStoreCid(couponTypeInfo.StoreId, couponTypeInfo.LimitStoreCid).Name + "类的商品时使用");
                                    }
                                }
                            }

                            if (couponTypeInfo.LimitProduct == 1)
                            {
                                List<OrderProductInfo> commonOrderProductList = Carts.GetCommonOrderProductList(storeCartInfo.SelectedOrderProductList);
                                if (!Coupons.IsSameCouponType(couponTypeInfo.CouponTypeId, Carts.GetPidList(commonOrderProductList)))
                                    throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能在购买指定商品时使用");
                            }
                        }

                        #endregion
                    }


                    string pidList = Carts.GetMallCartPidList(storeCartList);//商品id列表
                    List<ProductStockInfo> productStockList = Products.GetProductStockList(pidList);//商品库存列表
                    List<SinglePromotionInfo> singlePromotionList = new List<SinglePromotionInfo>();//单品促销活动列表

                    //循环店铺购物车列表，依次验证
                    foreach (StoreCartInfo storeCartInfo in storeCartList)
                    {
                        if (!storeCartInfo.IsSelected) continue;

                        #region 验证购物车商品

                        foreach (CartProductInfo cartProductInfo in storeCartInfo.CartProductList)
                        {
                            if (!cartProductInfo.IsSelected) continue;

                            OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                            //验证商品信息
                            PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                            if (partProductInfo == null)
                            {
                                throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                            }

                            //如果设置了日期价格，则使用日期价
                            partProductInfo.ShopPrice = Products.GetDayPrice(partProductInfo.Pid, orderProductInfo.BookStartTime, partProductInfo.ShopPrice);

                            if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                            {
                                throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                            }

                            //验证商品库存
                            ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                            if (productStockInfo.IsRecycle)
                            {
                                var days = (orderProductInfo.BookEndTime.Date - orderProductInfo.BookStartTime.Date).Days;
                                for (int i = 0; i < days; i++)
                                {
                                    var date = orderProductInfo.BookStartTime.AddDays(i);
                                    var history = ProductBookStockManager.GetBookStock(orderProductInfo.Pid, date);

                                    if (productStockInfo.RecycleNumber - history < orderProductInfo.BookNumber)
                                        throw new Exception("商品预定日期：" + date.ToShortDateString() + "库存不足");
                                }
                                if (orderProductInfo.BookStartTime.Date == DateTime.Now.Date)
                                    productStockInfo.Number -= orderProductInfo.BookNumber;

                            }
                            else
                            {
                                if (productStockInfo.Number < orderProductInfo.RealCount)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                                }
                                else
                                {
                                    productStockInfo.Number -= orderProductInfo.RealCount;
                                }
                            }

                            //验证买送促销活动
                            if (orderProductInfo.ExtCode2 > 0)
                            {
                                BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                if (buySendPromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                }
                                else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                }
                                else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                    orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                    Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                    if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                        }
                                        Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                    }
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                }
                            }

                            //验证单品促销活动
                            if (orderProductInfo.ExtCode1 > 0)
                            {
                                SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                if (singlePromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                }
                                if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                }
                                if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(data.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.IsStock == 1)
                                {
                                    SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                    if (temp == null)
                                    {
                                        temp = singlePromotionInfo;
                                        singlePromotionList.Add(temp);
                                    }

                                    if (temp.Stock < orderProductInfo.RealCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else
                                    {
                                        temp.Stock -= orderProductInfo.RealCount;
                                    }
                                }
                            }

                            //验证赠品促销活动
                            if (orderProductInfo.ExtCode3 > 0)
                            {
                                GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                if (giftPromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                }
                                else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                }
                                else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                    Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                {
                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                    Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (cartProductInfo.GiftList.Count > 0)
                                {
                                    List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                    if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                    foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                    {
                                        OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                        Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                        newGiftOrderProductList.Add(giftOrderProduct);
                                    }

                                    //验证赠品信息是否改变
                                    for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                    {
                                        OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                        OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                        if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                            newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                            newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                            newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                            newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                            newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                            newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                            newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                            newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                            newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                            newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                            newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                            newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                    }

                                    //验证赠品库存
                                    foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                    {
                                        ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                        if (stockInfo.Number < gift.RealCount)
                                        {
                                            if (stockInfo.Number == 0)
                                            {
                                                Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                if (cartProductInfo.GiftList.Count == 1)
                                                {
                                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                }
                                            }
                                            else
                                            {
                                                gift.RealCount = stockInfo.Number;
                                                Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                            }
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            stockInfo.Number -= gift.RealCount;
                                        }
                                    }
                                }
                            }
                        }

                        #endregion

                        #region 验证购物车套装

                        foreach (CartSuitInfo cartSuitInfo in storeCartInfo.CartSuitList)
                        {
                            if (!cartSuitInfo.IsSelected) continue;

                            SuitPromotionInfo suitPromotionInfo = Promotions.GetSuitPromotionByPmIdAndTime(cartSuitInfo.PmId, DateTime.Now);
                            if (suitPromotionInfo == null)
                            {
                                throw new Exception("商品" + cartSuitInfo.CartProductList[0].OrderProductInfo.Name + "的套装已经停止,请删除此套装");
                            }
                            else if (suitPromotionInfo.UserRankLower > partUserInfo.UserRid)
                            {
                                throw new Exception("你的用户等级太低，无法购买套装" + suitPromotionInfo.Name + "请删除此套装");
                            }
                            else if (suitPromotionInfo.QuotaUpper > 0 && cartSuitInfo.BuyCount > suitPromotionInfo.QuotaUpper)
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "要求每单最多购买" + suitPromotionInfo.QuotaUpper + "个");
                            }
                            else if (suitPromotionInfo.OnlyOnce == 1 && Promotions.IsJoinSuitPromotion(data.Uid, suitPromotionInfo.PmId))
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "要求每人最多购买1个");
                            }

                            List<OrderProductInfo> newSuitOrderProductList = new List<OrderProductInfo>();
                            foreach (ExtSuitProductInfo extSuitProductInfo in Promotions.GetExtSuitProductList(cartSuitInfo.PmId))
                            {
                                OrderProductInfo suitOrderProductInfo = Carts.BuildOrderProduct(extSuitProductInfo);
                                Carts.SetSuitOrderProduct(suitOrderProductInfo, cartSuitInfo.BuyCount, extSuitProductInfo.Number, extSuitProductInfo.Discount, suitPromotionInfo);
                                newSuitOrderProductList.Add(suitOrderProductInfo);
                            }
                            List<OrderProductInfo> oldSuitOrderProductList = new List<OrderProductInfo>();
                            foreach (CartProductInfo cartProductInfo in cartSuitInfo.CartProductList)
                            {
                                oldSuitOrderProductList.Add(cartProductInfo.OrderProductInfo);
                            }
                            if (newSuitOrderProductList.Count != oldSuitOrderProductList.Count)
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "已经改变，请删除此套装后重新下单");
                            }
                            else
                            {
                                for (int i = 0; i < newSuitOrderProductList.Count; i++)
                                {
                                    OrderProductInfo newSuitOrderProductInfo = newSuitOrderProductList[i];
                                    OrderProductInfo oldSuitOrderProductInfo = oldSuitOrderProductList[i];
                                    if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                        newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                        newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                        newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                        newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                        newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                        newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                        newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                        newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                        newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("套装" + suitPromotionInfo.Name + "已经改变，请删除此套装后重新下单");
                                    }
                                }

                                foreach (CartProductInfo cartProductInfo in cartSuitInfo.CartProductList)
                                {
                                    OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                    //验证商品信息
                                    PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                    if (partProductInfo == null)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "已经下架，请删除此套装");
                                    }
                                    if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "信息有变化，请删除套装后重新添加");
                                    }

                                    //验证商品库存
                                    ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                    if (productStockInfo.Number < orderProductInfo.RealCount)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "库存不足");
                                    }
                                    else
                                    {
                                        productStockInfo.Number -= orderProductInfo.RealCount;
                                    }

                                    //验证赠品促销活动
                                    if (orderProductInfo.ExtCode3 > 0)
                                    {
                                        GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                        if (giftPromotionInfo == null)
                                        {
                                            throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此套装后重新添加");
                                        }
                                        else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                        {
                                            throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                        }
                                        else if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                            if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                            {
                                                throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                            }
                                            List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                            foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                            {
                                                OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                                Carts.SetGiftOrderProduct(giftOrderProduct, 2, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                                newGiftOrderProductList.Add(giftOrderProduct);
                                            }

                                            //验证赠品信息是否改变
                                            for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                            {
                                                OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                                OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                                if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                    newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                    newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                    newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                    newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                    newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                    newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                    newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                    newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                    newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2)
                                                {
                                                    throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                                }
                                            }

                                            //验证赠品库存
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                                if (stockInfo.Number < gift.RealCount)
                                                {
                                                    if (stockInfo.Number == 0)
                                                    {
                                                        Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                        if (cartProductInfo.GiftList.Count == 1)
                                                        {
                                                            orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                            Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                        }
                                                    }
                                                    else
                                                    {
                                                        gift.RealCount = stockInfo.Number;
                                                        Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                    }
                                                    throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                                }
                                                else
                                                {
                                                    stockInfo.Number -= gift.RealCount;
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                        }

                        #endregion

                        #region 验证购物车满赠

                        foreach (CartFullSendInfo cartFullSendInfo in storeCartInfo.CartFullSendList)
                        {
                            if (cartFullSendInfo.IsEnough && cartFullSendInfo.FullSendMinorOrderProductInfo != null)
                            {
                                if (cartFullSendInfo.FullSendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    throw new Exception("你的用户等级太低，无法参加商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "的满赠促销活动,请先删除此赠品");
                                }
                                if (cartFullSendInfo.FullSendMinorOrderProductInfo.DiscountPrice != cartFullSendInfo.FullSendPromotionInfo.AddMoney)
                                {
                                    throw new Exception("商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "的满赠促销活动金额错误,请删除后重新添加");
                                }
                                if (!Promotions.IsExistFullSendProduct(cartFullSendInfo.FullSendPromotionInfo.PmId, cartFullSendInfo.FullSendMinorOrderProductInfo.Pid, 1))
                                {
                                    throw new Exception("商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "不是满赠赠品,请删除后再结账");
                                }

                                ProductStockInfo productStockInfo = Products.GetProductStock(cartFullSendInfo.FullSendMinorOrderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < cartFullSendInfo.FullSendMinorOrderProductInfo.RealCount)
                                {
                                    throw new Exception("满赠赠品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "库存不足,请删除此满赠赠品");
                                }
                                else
                                {
                                    productStockInfo.Number -= cartFullSendInfo.FullSendMinorOrderProductInfo.RealCount;
                                }
                            }

                            foreach (CartProductInfo cartProductInfo in cartFullSendInfo.FullSendMainCartProductList)
                            {
                                #region 验证

                                if (cartProductInfo.IsSelected) continue;

                                OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                //验证商品信息
                                PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                if (partProductInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                                }

                                //如果设置了日期价格，则使用日期价
                                partProductInfo.ShopPrice = Products.GetDayPrice(partProductInfo.Pid, orderProductInfo.BookStartTime, partProductInfo.ShopPrice);

                                if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                                }

                                //验证商品库存
                                ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < orderProductInfo.RealCount)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                                }
                                else
                                {
                                    productStockInfo.Number -= orderProductInfo.RealCount;
                                }

                                //验证买送促销活动
                                if (orderProductInfo.ExtCode2 > 0)
                                {
                                    BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                    if (buySendPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                        orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                        Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                        if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                            }
                                            Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                        }
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                    }
                                }

                                //验证单品促销活动
                                if (orderProductInfo.ExtCode1 > 0)
                                {
                                    SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (singlePromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(data.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.IsStock == 1)
                                    {
                                        SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                        if (temp == null)
                                        {
                                            temp = singlePromotionInfo;
                                            singlePromotionList.Add(temp);
                                        }

                                        if (temp.Stock < orderProductInfo.RealCount)
                                        {
                                            orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                            orderProductInfo.PayCredits = 0;
                                            orderProductInfo.CouponTypeId = 0;
                                            orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                            Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                            throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            temp.Stock -= orderProductInfo.RealCount;
                                        }
                                    }
                                }

                                //验证赠品促销活动
                                if (orderProductInfo.ExtCode3 > 0)
                                {
                                    GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (giftPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                        if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                        List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                        foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                        {
                                            OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                            Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                            newGiftOrderProductList.Add(giftOrderProduct);
                                        }

                                        //验证赠品信息是否改变
                                        for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                        {
                                            OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                            OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                            if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                                newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                                newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                                newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                            {
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                            }
                                        }

                                        //验证赠品库存
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                            if (stockInfo.Number < gift.RealCount)
                                            {
                                                if (stockInfo.Number == 0)
                                                {
                                                    Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                    if (cartProductInfo.GiftList.Count == 1)
                                                    {
                                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                    }
                                                }
                                                else
                                                {
                                                    gift.RealCount = stockInfo.Number;
                                                    Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                }
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                            }
                                            else
                                            {
                                                stockInfo.Number -= gift.RealCount;
                                            }
                                        }
                                    }
                                }

                                #endregion
                            }
                        }

                        #endregion

                        #region 验证购物车满减

                        foreach (CartFullCutInfo cartFullCutInfo in storeCartInfo.CartFullCutList)
                        {
                            if (cartFullCutInfo.FullCutPromotionInfo.UserRankLower > partUserInfo.UserRid)
                            {
                                List<OrderProductInfo> updateFullCutOrderProductList = new List<OrderProductInfo>();
                                foreach (CartProductInfo cartProductInfo in cartFullCutInfo.FullCutCartProductList)
                                {
                                    cartProductInfo.OrderProductInfo.ExtCode5 = -1 * cartProductInfo.OrderProductInfo.ExtCode5;
                                    updateFullCutOrderProductList.Add(cartProductInfo.OrderProductInfo);
                                }
                                Carts.UpdateOrderProductFullCut(updateFullCutOrderProductList);
                                throw new Exception("你的用户等级太低，无法参加" + cartFullCutInfo.FullCutPromotionInfo.Name + "满减促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                            }

                            foreach (CartProductInfo cartProductInfo in cartFullCutInfo.FullCutCartProductList)
                            {
                                #region 验证

                                if (cartProductInfo.IsSelected) continue;

                                OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                //验证商品信息
                                PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                if (partProductInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                                }
                                //如果设置了日期价格，则使用日期价
                                partProductInfo.ShopPrice = Products.GetDayPrice(partProductInfo.Pid, orderProductInfo.BookStartTime, partProductInfo.ShopPrice);

                                if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                                }

                                //验证商品库存
                                ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < orderProductInfo.RealCount)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                                }
                                else
                                {
                                    productStockInfo.Number -= orderProductInfo.RealCount;
                                }

                                //验证买送促销活动
                                if (orderProductInfo.ExtCode2 > 0)
                                {
                                    BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                    if (buySendPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                        orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                        Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                        if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                            }
                                            Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                        }
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                    }
                                }

                                //验证单品促销活动
                                if (orderProductInfo.ExtCode1 > 0)
                                {
                                    SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (singlePromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(data.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.IsStock == 1)
                                    {
                                        SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                        if (temp == null)
                                        {
                                            temp = singlePromotionInfo;
                                            singlePromotionList.Add(temp);
                                        }

                                        if (temp.Stock < orderProductInfo.RealCount)
                                        {
                                            orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                            orderProductInfo.PayCredits = 0;
                                            orderProductInfo.CouponTypeId = 0;
                                            orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                            Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                            throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            temp.Stock -= orderProductInfo.RealCount;
                                        }
                                    }
                                }

                                //验证赠品促销活动
                                if (orderProductInfo.ExtCode3 > 0)
                                {
                                    GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (giftPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                        if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                        List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                        foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                        {
                                            OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                            Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                            newGiftOrderProductList.Add(giftOrderProduct);
                                        }

                                        //验证赠品信息是否改变
                                        for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                        {
                                            OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                            OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                            if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                                newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                                newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                                newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                            {
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                            }
                                        }

                                        //验证赠品库存
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                            if (stockInfo.Number < gift.RealCount)
                                            {
                                                if (stockInfo.Number == 0)
                                                {
                                                    Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                    if (cartProductInfo.GiftList.Count == 1)
                                                    {
                                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                    }
                                                }
                                                else
                                                {
                                                    gift.RealCount = stockInfo.Number;
                                                    Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                }
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                            }
                                            else
                                            {
                                                stockInfo.Number -= gift.RealCount;
                                            }
                                        }
                                    }
                                }

                                #endregion
                            }
                        }

                        #endregion
                    }
                    if (Carts.SumMallCartFullCut(storeCartList) != fullCut)
                        throw new Exception("满减金额不正确,请刷新页面重新提交");

                    //验证已经通过,进行订单保存
                    int pCount = 0;
                    string oidList = "";
                    foreach (StoreCartInfo storeCartInfo in storeCartList)
                    {
                        List<SinglePromotionInfo> storeSinglePromotionList = singlePromotionList.FindAll(x => x.StoreId == storeCartInfo.StoreInfo.StoreId);
                        List<CouponInfo> storeCouponList = couponList.FindAll(x => x.StoreId == storeCartInfo.StoreInfo.StoreId);
                        int storeFullCut = Carts.SumFullCut(storeCartInfo);
                        int rankDiscount = UserRanks.GetUserRankById(partUserInfo.UserRid)?.RankDiscount ?? 0;

                        OrderInfo orderInfo = Orders.CreateOrder(partUserInfo, storeCartInfo.StoreInfo, storeCartInfo.SelectedOrderProductList,
                            storeSinglePromotionList, fullShipAddressInfo, payMode == 0 ? 0 : 1,
                            ref payCreditCount, storeCouponList, storeFullCut, buyerRemark, bestTime, WebHelper.GetIP(),
                            data.BookStartTime.ToString("yyyy-MM-dd HH:mm:ss"), data.BookEndTime.ToString("yyyy-MM-dd HH:mm:ss"),
                            rankDiscount: rankDiscount);

                        if (orderInfo != null)
                        {
                            #region 计算余额，并更新付款金额、订单状态   2017-11-22 wh add

                            //计算余额
                            if (payCash > orderInfo.SurplusMoney)
                                payCash = orderInfo.SurplusMoney;   //在线付款额度 必须 >= 余额支付额度

                            decimal surplusMoney = orderInfo.SurplusMoney - payCash;
                            int orderState = orderInfo.OrderState;

                            if (surplusMoney <= 0)
                            {
                                orderState = (int)OrderState.PreProducting; //不需要调用微信支付,改为：备货中
                                                                            //更新付款金额,订单状态
                                int count = Manager.Manager.Order.OrderManager.UpdatePayCash(orderInfo.Oid, payCash, surplusMoney, orderState);
                                //余额扣款
                                if (count > 0)
                                    CashOrders.ConsumeCash(orderInfo.Uid, orderInfo.StoreId, payCash, "");

                                //达达自动配送
                                Ships.AutoShip(orderInfo, DateTime.Now.ToString());
                            }
                            else
                                Manager.Manager.Order.OrderManager.UpdatePayCash(orderInfo.Oid, payCash, surplusMoney, orderState);

                            #endregion


                            //创建订单处理
                            OrderActions.CreateOrderAction(new OrderActionInfo()
                            {
                                Oid = orderInfo.Oid,
                                Uid = data.Uid,
                                RealName = Users.GetPartUserById(data.Uid)?.NickName,
                                ActionType = (int)OrderActionType.Submit,
                                ActionTime = DateTime.Now,
                                ActionDes = orderInfo.OrderState == (int)OrderState.WaitPaying ? "您提交了订单，等待您付款" : "您提交了订单，请等待系统确认"
                            });
                            //删除剩余的满赠赠品
                            if (storeCartInfo.RemainedOrderProductList.Count > 0)
                            {
                                List<OrderProductInfo> delOrderProductList = Carts.GetFullSendMinorOrderProductList(storeCartInfo.RemainedOrderProductList);
                                if (delOrderProductList.Count > 0)
                                {
                                    Carts.DeleteOrderProductList(delOrderProductList);
                                    pCount += Carts.SumOrderProductCount(storeCartInfo.RemainedOrderProductList) - delOrderProductList.Count;
                                }
                            }
                            oidList += orderInfo.Oid + ",";
                        }
                        else
                        {
                            throw new Exception("提交失败，请联系管理员");
                        }
                    }
                    //目前只支持单个店铺订单，所以这里应该只有一个订单号
                    return oidList.TrimEnd(',');

                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }

        }
        /// <summary>
        /// 创建线下订单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public string SubmitSessionOrder(CreateBackEndOrderInputDto data)
        {

            try
            {
                lock (_locker)
                {
                    //if (!string.IsNullOrEmpty(data.Sid))
                    //    Carts.UpdateCartUidBySid(data.Uid, data.Sid);
                    string selectedCartItemKeyList = string.Empty;//选中的购物车项键列表,取消勾选付款，餐饮需求
                    int saId = data.SaId;//配送地址id
                    int payMode = 1;//支付方式
                    int payCreditCount = data.PayCreditCount;//支付积分
                    string[] couponIdList = StringHelper.SplitString(data.CouponIdList ?? "");//客户已经激活的优惠劵
                    string[] couponSNList = StringHelper.SplitString(data.CouponSNList ?? "");//客户还未激活的优惠劵
                    int fullCut = data.FullCut;//满减金额
                    DateTime bestTime = new DateTime(1970, 1, 1);//最佳配送时间
                    string buyerRemark = data.BuyerRemark ?? "";//买家备注

                    var partUserInfo = Users.GetPartUserById(data.Uid);


                    //验证买家备注的内容长度
                    if (StringHelper.GetStringLength(buyerRemark) > 125)
                        throw new Exception("备注最多填写125个字");

                    //验证配送地址是否为空
                    FullShipAddressInfo fullShipAddressInfo = ShipAddresses.GetFullShipAddressBySAId(saId, data.Uid);
                    if (fullShipAddressInfo == null)
                        fullShipAddressInfo = new FullShipAddressInfo();

                    //购物车商品列表
                    List<OrderProductInfo> orderProductList = Carts.GetCartProductList(data.Sid).Where(t => CartManager.CheckIsPrint(t.RecordId)).ToList();
                    if (orderProductList.Count < 1)
                        throw new Exception("购物车中没有商品");

                    //店铺购物车列表
                    List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selectedCartItemKeyList), orderProductList);
                    if (Carts.SumMallCartOrderProductCount(storeCartList) < 1)
                        throw new Exception("购物车中没有选中的商品");

                    //被选中的店铺数量
                    int selectedStoreCount = Carts.SumSelectedStoreCount(storeCartList);
                    var creditConfig = StoreConfigManager.GetStoreCreditConfigInfo(partUserInfo.RegisterFromStoreId);
                    //验证支付积分
                    if (payCreditCount > 0)
                    {
                        if (payCreditCount > partUserInfo.PayCredits)
                            throw new Exception("你使用的" + (creditConfig?.PayCreditName ?? "") + "数超过你所拥有的" + partUserInfo.PayCredits + "数");
                        if (payCreditCount > (creditConfig?.OrderMaxUsePayCredits ?? 0) * selectedStoreCount)
                            throw new Exception("此笔订单最多使用" + (creditConfig?.OrderMaxUsePayCredits ?? 0) * selectedStoreCount + "个" + (creditConfig?.PayCreditName ?? ""));
                    }

                    //验证 余额支付
                    decimal payCash = data.PayCash;     //支付的余额
                    Trace.TraceInformation($"OrderController.cs 余额支付测试: 使用余额：{payCash}");
                    if (payCash > 0)
                    {
                        var userCash = Manager.Manager.User.UserCashManager.GetInfo(data.Uid);
                        decimal balance = CashOrders.DecodeCash(userCash.Cash);

                        if (userCash == null || payCash > balance)
                            throw new Exception($"余额支付不能超过{balance}元");
                    }
                    if (payCash < 0) payCash = 0;


                    //验证优惠劵
                    List<CouponInfo> couponList = new List<CouponInfo>();
                    if (couponIdList.Length > 0)
                    {
                        foreach (string couponId in couponIdList)
                        {
                            int tempCouponId = TypeHelper.StringToInt(couponId);
                            if (tempCouponId > 0)
                            {
                                CouponInfo couponInfo = Coupons.GetCouponByCouponId(tempCouponId);
                                if (couponInfo == null)
                                    throw new Exception("优惠劵不存在");
                                else
                                    couponList.Add(couponInfo);
                            }
                        }
                    }
                    if (couponSNList.Length > 0)
                    {
                        foreach (string couponSN in couponSNList)
                        {
                            if (!string.IsNullOrWhiteSpace(couponSN))
                            {
                                CouponInfo couponInfo = Coupons.GetCouponByCouponSN(couponSN);
                                if (couponInfo == null)
                                    throw new Exception("优惠劵" + couponSN + "不存在");
                                else
                                    couponList.Add(couponInfo);
                            }
                        }
                    }
                    foreach (CouponInfo couponInfo in couponList)
                    {
                        #region  验证

                        CouponTypeInfo couponTypeInfo = Coupons.GetCouponTypeById(couponInfo.CouponTypeId);
                        if (couponTypeInfo == null)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵类型不存在");
                        }
                        else if (couponTypeInfo.State == 0)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵类型已关闭");
                        }
                        else if (couponTypeInfo.UseExpireTime == 0 && couponTypeInfo.UseStartTime > DateTime.Now)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵还未到使用时间");
                        }
                        else if (couponTypeInfo.UseExpireTime == 0 && couponTypeInfo.UseEndTime <= DateTime.Now)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵已过期");
                        }
                        else if (couponTypeInfo.UseExpireTime > 0 && couponInfo.ActivateTime <= DateTime.Now.AddDays(-1 * couponTypeInfo.UseExpireTime))
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵已过期");
                        }
                        else if (couponTypeInfo.UserRankLower > partUserInfo.UserRid)
                        {
                            throw new Exception("你的用户等级太低，不能使用编号为" + couponInfo.CouponSN + "优惠劵");
                        }
                        else if (couponList.Count > 1 && couponTypeInfo.UseMode == 1)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵不能叠加使用");
                        }
                        else
                        {
                            StoreCartInfo storeCartInfo = storeCartList.Find(x => x.StoreInfo.StoreId == couponTypeInfo.StoreId);

                            if (storeCartInfo == null)
                            {
                                throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能购买对应店铺的商品");
                            }

                            if (couponTypeInfo.OrderAmountLower > Carts.SumOrderProductAmount(storeCartInfo.SelectedOrderProductList))
                            {
                                throw new Exception("订单金额太低，不能使用编号为" + couponInfo.CouponSN + "优惠劵");
                            }

                            if (couponTypeInfo.LimitStoreCid > 0)
                            {
                                foreach (OrderProductInfo orderProductInfo in storeCartInfo.SelectedOrderProductList)
                                {
                                    if (orderProductInfo.Type == 0 && orderProductInfo.StoreCid != couponTypeInfo.LimitStoreCid)
                                    {
                                        throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能在购买" + Stores.GetStoreClassByStoreIdAndStoreCid(couponTypeInfo.StoreId, couponTypeInfo.LimitStoreCid).Name + "类的商品时使用");
                                    }
                                }
                            }

                            if (couponTypeInfo.LimitProduct == 1)
                            {
                                List<OrderProductInfo> commonOrderProductList = Carts.GetCommonOrderProductList(storeCartInfo.SelectedOrderProductList);
                                if (!Coupons.IsSameCouponType(couponTypeInfo.CouponTypeId, Carts.GetPidList(commonOrderProductList)))
                                    throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能在购买指定商品时使用");
                            }
                        }

                        #endregion
                    }


                    string pidList = Carts.GetMallCartPidList(storeCartList);//商品id列表
                    List<ProductStockInfo> productStockList = Products.GetProductStockList(pidList);//商品库存列表
                    List<SinglePromotionInfo> singlePromotionList = new List<SinglePromotionInfo>();//单品促销活动列表

                    //循环店铺购物车列表，依次验证
                    foreach (StoreCartInfo storeCartInfo in storeCartList)
                    {
                        if (!storeCartInfo.IsSelected) continue;

                        #region 验证购物车商品

                        foreach (CartProductInfo cartProductInfo in storeCartInfo.CartProductList)
                        {
                            if (!cartProductInfo.IsSelected) continue;

                            OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                            //验证商品信息
                            PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                            if (partProductInfo == null)
                            {
                                throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                            }
                            if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                            {
                                throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                            }

                            //验证商品库存
                            ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                            if (productStockInfo.IsRecycle)
                            {
                                var days = (orderProductInfo.BookEndTime.Date - orderProductInfo.BookStartTime.Date).Days;
                                for (int i = 0; i < days; i++)
                                {
                                    var date = orderProductInfo.BookStartTime.AddDays(i);
                                    var history = ProductBookStockManager.GetBookStock(orderProductInfo.Pid, date);

                                    if (productStockInfo.RecycleNumber - history < orderProductInfo.BookNumber)
                                        throw new Exception("商品预定日期：" + date.ToShortDateString() + "库存不足");
                                }
                                if (orderProductInfo.BookStartTime.Date == DateTime.Now.Date)
                                    productStockInfo.Number -= orderProductInfo.BookNumber;

                            }
                            else
                            {
                                if (productStockInfo.Number < orderProductInfo.RealCount)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                                }
                                else
                                {
                                    productStockInfo.Number -= orderProductInfo.RealCount;
                                }
                            }

                            //验证买送促销活动
                            if (orderProductInfo.ExtCode2 > 0)
                            {
                                BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                if (buySendPromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                }
                                else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                }
                                else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                    orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                    Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                    if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                        }
                                        Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                    }
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                }
                            }

                            //验证单品促销活动
                            if (orderProductInfo.ExtCode1 > 0)
                            {
                                SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                if (singlePromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                }
                                if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                }
                                if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(data.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                {
                                    orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                    orderProductInfo.PayCredits = 0;
                                    orderProductInfo.CouponTypeId = 0;
                                    orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                    Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                    throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                if (singlePromotionInfo.IsStock == 1)
                                {
                                    SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                    if (temp == null)
                                    {
                                        temp = singlePromotionInfo;
                                        singlePromotionList.Add(temp);
                                    }

                                    if (temp.Stock < orderProductInfo.RealCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else
                                    {
                                        temp.Stock -= orderProductInfo.RealCount;
                                    }
                                }
                            }

                            //验证赠品促销活动
                            if (orderProductInfo.ExtCode3 > 0)
                            {
                                GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                if (giftPromotionInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                }
                                else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                }
                                else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                    Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                    throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                {
                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                    Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                    throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                }
                                else if (cartProductInfo.GiftList.Count > 0)
                                {
                                    List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                    if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                    foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                    {
                                        OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                        Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                        newGiftOrderProductList.Add(giftOrderProduct);
                                    }

                                    //验证赠品信息是否改变
                                    for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                    {
                                        OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                        OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                        if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                            newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                            newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                            newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                            newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                            newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                            newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                            newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                            newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                            newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                            newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                            newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                            newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                    }

                                    //验证赠品库存
                                    foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                    {
                                        ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                        if (stockInfo.Number < gift.RealCount)
                                        {
                                            if (stockInfo.Number == 0)
                                            {
                                                Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                if (cartProductInfo.GiftList.Count == 1)
                                                {
                                                    orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                    Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                }
                                            }
                                            else
                                            {
                                                gift.RealCount = stockInfo.Number;
                                                Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                            }
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            stockInfo.Number -= gift.RealCount;
                                        }
                                    }
                                }
                            }
                        }

                        #endregion

                        #region 验证购物车套装

                        foreach (CartSuitInfo cartSuitInfo in storeCartInfo.CartSuitList)
                        {
                            if (!cartSuitInfo.IsSelected) continue;

                            SuitPromotionInfo suitPromotionInfo = Promotions.GetSuitPromotionByPmIdAndTime(cartSuitInfo.PmId, DateTime.Now);
                            if (suitPromotionInfo == null)
                            {
                                throw new Exception("商品" + cartSuitInfo.CartProductList[0].OrderProductInfo.Name + "的套装已经停止,请删除此套装");
                            }
                            else if (suitPromotionInfo.UserRankLower > partUserInfo.UserRid)
                            {
                                throw new Exception("你的用户等级太低，无法购买套装" + suitPromotionInfo.Name + "请删除此套装");
                            }
                            else if (suitPromotionInfo.QuotaUpper > 0 && cartSuitInfo.BuyCount > suitPromotionInfo.QuotaUpper)
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "要求每单最多购买" + suitPromotionInfo.QuotaUpper + "个");
                            }
                            else if (suitPromotionInfo.OnlyOnce == 1 && Promotions.IsJoinSuitPromotion(data.Uid, suitPromotionInfo.PmId))
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "要求每人最多购买1个");
                            }

                            List<OrderProductInfo> newSuitOrderProductList = new List<OrderProductInfo>();
                            foreach (ExtSuitProductInfo extSuitProductInfo in Promotions.GetExtSuitProductList(cartSuitInfo.PmId))
                            {
                                OrderProductInfo suitOrderProductInfo = Carts.BuildOrderProduct(extSuitProductInfo);
                                Carts.SetSuitOrderProduct(suitOrderProductInfo, cartSuitInfo.BuyCount, extSuitProductInfo.Number, extSuitProductInfo.Discount, suitPromotionInfo);
                                newSuitOrderProductList.Add(suitOrderProductInfo);
                            }
                            List<OrderProductInfo> oldSuitOrderProductList = new List<OrderProductInfo>();
                            foreach (CartProductInfo cartProductInfo in cartSuitInfo.CartProductList)
                            {
                                oldSuitOrderProductList.Add(cartProductInfo.OrderProductInfo);
                            }
                            if (newSuitOrderProductList.Count != oldSuitOrderProductList.Count)
                            {
                                throw new Exception("套装" + suitPromotionInfo.Name + "已经改变，请删除此套装后重新下单");
                            }
                            else
                            {
                                for (int i = 0; i < newSuitOrderProductList.Count; i++)
                                {
                                    OrderProductInfo newSuitOrderProductInfo = newSuitOrderProductList[i];
                                    OrderProductInfo oldSuitOrderProductInfo = oldSuitOrderProductList[i];
                                    if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                        newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                        newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                        newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                        newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                        newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                        newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                        newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                        newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                        newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("套装" + suitPromotionInfo.Name + "已经改变，请删除此套装后重新下单");
                                    }
                                }

                                foreach (CartProductInfo cartProductInfo in cartSuitInfo.CartProductList)
                                {
                                    OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                    //验证商品信息
                                    PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                    if (partProductInfo == null)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "已经下架，请删除此套装");
                                    }
                                    if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "信息有变化，请删除套装后重新添加");
                                    }

                                    //验证商品库存
                                    ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                    if (productStockInfo.Number < orderProductInfo.RealCount)
                                    {
                                        throw new Exception("套装中的商品" + orderProductInfo.Name + "库存不足");
                                    }
                                    else
                                    {
                                        productStockInfo.Number -= orderProductInfo.RealCount;
                                    }

                                    //验证赠品促销活动
                                    if (orderProductInfo.ExtCode3 > 0)
                                    {
                                        GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                        if (giftPromotionInfo == null)
                                        {
                                            throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此套装后重新添加");
                                        }
                                        else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                        {
                                            throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                        }
                                        else if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                            if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                            {
                                                throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                            }
                                            List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                            foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                            {
                                                OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                                Carts.SetGiftOrderProduct(giftOrderProduct, 2, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                                newGiftOrderProductList.Add(giftOrderProduct);
                                            }

                                            //验证赠品信息是否改变
                                            for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                            {
                                                OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                                OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                                if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                    newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                    newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                    newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                    newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                    newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                    newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                    newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                    newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                    newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2)
                                                {
                                                    throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品已经改变，请删除此套装后重新添加");
                                                }
                                            }

                                            //验证赠品库存
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                                if (stockInfo.Number < gift.RealCount)
                                                {
                                                    if (stockInfo.Number == 0)
                                                    {
                                                        Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                        if (cartProductInfo.GiftList.Count == 1)
                                                        {
                                                            orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                            Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                        }
                                                    }
                                                    else
                                                    {
                                                        gift.RealCount = stockInfo.Number;
                                                        Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                    }
                                                    throw new Exception("套装中的商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                                }
                                                else
                                                {
                                                    stockInfo.Number -= gift.RealCount;
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                        }

                        #endregion

                        #region 验证购物车满赠

                        foreach (CartFullSendInfo cartFullSendInfo in storeCartInfo.CartFullSendList)
                        {
                            if (cartFullSendInfo.IsEnough && cartFullSendInfo.FullSendMinorOrderProductInfo != null)
                            {
                                if (cartFullSendInfo.FullSendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                {
                                    throw new Exception("你的用户等级太低，无法参加商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "的满赠促销活动,请先删除此赠品");
                                }
                                if (cartFullSendInfo.FullSendMinorOrderProductInfo.DiscountPrice != cartFullSendInfo.FullSendPromotionInfo.AddMoney)
                                {
                                    throw new Exception("商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "的满赠促销活动金额错误,请删除后重新添加");
                                }
                                if (!Promotions.IsExistFullSendProduct(cartFullSendInfo.FullSendPromotionInfo.PmId, cartFullSendInfo.FullSendMinorOrderProductInfo.Pid, 1))
                                {
                                    throw new Exception("商品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "不是满赠赠品,请删除后再结账");
                                }

                                ProductStockInfo productStockInfo = Products.GetProductStock(cartFullSendInfo.FullSendMinorOrderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < cartFullSendInfo.FullSendMinorOrderProductInfo.RealCount)
                                {
                                    throw new Exception("满赠赠品" + cartFullSendInfo.FullSendMinorOrderProductInfo.Name + "库存不足,请删除此满赠赠品");
                                }
                                else
                                {
                                    productStockInfo.Number -= cartFullSendInfo.FullSendMinorOrderProductInfo.RealCount;
                                }
                            }

                            foreach (CartProductInfo cartProductInfo in cartFullSendInfo.FullSendMainCartProductList)
                            {
                                #region 验证

                                if (cartProductInfo.IsSelected) continue;

                                OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                //验证商品信息
                                PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                if (partProductInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                                }
                                if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                                }

                                //验证商品库存
                                ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < orderProductInfo.RealCount)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                                }
                                else
                                {
                                    productStockInfo.Number -= orderProductInfo.RealCount;
                                }

                                //验证买送促销活动
                                if (orderProductInfo.ExtCode2 > 0)
                                {
                                    BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                    if (buySendPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                        orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                        Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                        if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                            }
                                            Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                        }
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                    }
                                }

                                //验证单品促销活动
                                if (orderProductInfo.ExtCode1 > 0)
                                {
                                    SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (singlePromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(data.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.IsStock == 1)
                                    {
                                        SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                        if (temp == null)
                                        {
                                            temp = singlePromotionInfo;
                                            singlePromotionList.Add(temp);
                                        }

                                        if (temp.Stock < orderProductInfo.RealCount)
                                        {
                                            orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                            orderProductInfo.PayCredits = 0;
                                            orderProductInfo.CouponTypeId = 0;
                                            orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                            Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                            throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            temp.Stock -= orderProductInfo.RealCount;
                                        }
                                    }
                                }

                                //验证赠品促销活动
                                if (orderProductInfo.ExtCode3 > 0)
                                {
                                    GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (giftPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                        if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                        List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                        foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                        {
                                            OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                            Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                            newGiftOrderProductList.Add(giftOrderProduct);
                                        }

                                        //验证赠品信息是否改变
                                        for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                        {
                                            OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                            OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                            if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                                newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                                newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                                newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                            {
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                            }
                                        }

                                        //验证赠品库存
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                            if (stockInfo.Number < gift.RealCount)
                                            {
                                                if (stockInfo.Number == 0)
                                                {
                                                    Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                    if (cartProductInfo.GiftList.Count == 1)
                                                    {
                                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                    }
                                                }
                                                else
                                                {
                                                    gift.RealCount = stockInfo.Number;
                                                    Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                }
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                            }
                                            else
                                            {
                                                stockInfo.Number -= gift.RealCount;
                                            }
                                        }
                                    }
                                }

                                #endregion
                            }
                        }

                        #endregion

                        #region 验证购物车满减

                        foreach (CartFullCutInfo cartFullCutInfo in storeCartInfo.CartFullCutList)
                        {
                            if (cartFullCutInfo.FullCutPromotionInfo.UserRankLower > partUserInfo.UserRid)
                            {
                                List<OrderProductInfo> updateFullCutOrderProductList = new List<OrderProductInfo>();
                                foreach (CartProductInfo cartProductInfo in cartFullCutInfo.FullCutCartProductList)
                                {
                                    cartProductInfo.OrderProductInfo.ExtCode5 = -1 * cartProductInfo.OrderProductInfo.ExtCode5;
                                    updateFullCutOrderProductList.Add(cartProductInfo.OrderProductInfo);
                                }
                                Carts.UpdateOrderProductFullCut(updateFullCutOrderProductList);
                                throw new Exception("你的用户等级太低，无法参加" + cartFullCutInfo.FullCutPromotionInfo.Name + "满减促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                            }

                            foreach (CartProductInfo cartProductInfo in cartFullCutInfo.FullCutCartProductList)
                            {
                                #region 验证

                                if (cartProductInfo.IsSelected) continue;

                                OrderProductInfo orderProductInfo = cartProductInfo.OrderProductInfo;

                                //验证商品信息
                                PartProductInfo partProductInfo = Products.GetPartProductById(orderProductInfo.Pid);
                                if (partProductInfo == null)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "已经下架，请删除此商品");
                                }
                                if (orderProductInfo.Name != partProductInfo.Name || orderProductInfo.ShopPrice != partProductInfo.ShopPrice || orderProductInfo.MarketPrice != partProductInfo.MarketPrice || orderProductInfo.CostPrice != partProductInfo.CostPrice || orderProductInfo.Weight != partProductInfo.Weight || orderProductInfo.PSN != partProductInfo.PSN)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "信息有变化，请删除后重新添加");
                                }

                                //验证商品库存
                                ProductStockInfo productStockInfo = Products.GetProductStock(orderProductInfo.Pid, productStockList);
                                if (productStockInfo.Number < orderProductInfo.RealCount)
                                {
                                    throw new Exception("商品" + orderProductInfo.Name + "库存不足");
                                }
                                else
                                {
                                    productStockInfo.Number -= orderProductInfo.RealCount;
                                }

                                //验证买送促销活动
                                if (orderProductInfo.ExtCode2 > 0)
                                {
                                    BuySendPromotionInfo buySendPromotionInfo = Promotions.GetBuySendPromotion(orderProductInfo.BuyCount, orderProductInfo.StoreId, orderProductInfo.Pid, DateTime.Now);
                                    if (buySendPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.PmId != orderProductInfo.ExtCode2)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经替换，请删除此商品后重新添加");
                                    }
                                    else if (buySendPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.RealCount = orderProductInfo.BuyCount;
                                        orderProductInfo.ExtCode2 = -1 * orderProductInfo.ExtCode2;
                                        Carts.UpdateOrderProductBuySend(new List<OrderProductInfo>() { orderProductInfo });
                                        if (cartProductInfo.GiftList.Count > 0)
                                        {
                                            foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                            {
                                                gift.RealCount = orderProductInfo.BuyCount * gift.ExtCode2;
                                            }
                                            Carts.UpdateOrderProductCount(cartProductInfo.GiftList);
                                        }
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的买送促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (orderProductInfo.RealCount != (orderProductInfo.BuyCount + (orderProductInfo.BuyCount / buySendPromotionInfo.BuyCount) * buySendPromotionInfo.SendCount))
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的买送促销活动已经改变，请删除此商品后重新添加");
                                    }
                                }

                                //验证单品促销活动
                                if (orderProductInfo.ExtCode1 > 0)
                                {
                                    SinglePromotionInfo singlePromotionInfo = Promotions.GetSinglePromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (singlePromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经停止，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.PayCredits != orderProductInfo.PayCredits || singlePromotionInfo.CouponTypeId != orderProductInfo.CouponTypeId)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动已经改变，请删除此商品后重新添加");
                                    }
                                    if (singlePromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的单品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaLower > 0 && orderProductInfo.BuyCount < singlePromotionInfo.QuotaLower)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最少购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > singlePromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动要求每单最多购买" + singlePromotionInfo.QuotaLower + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.AllowBuyCount > 0 && Promotions.GetSinglePromotionProductBuyCount(data.Uid, singlePromotionInfo.PmId) + orderProductInfo.RealCount > singlePromotionInfo.AllowBuyCount)
                                    {
                                        orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                        orderProductInfo.PayCredits = 0;
                                        orderProductInfo.CouponTypeId = 0;
                                        orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                        Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                        throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动每个人最多购买" + singlePromotionInfo.AllowBuyCount + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    if (singlePromotionInfo.IsStock == 1)
                                    {
                                        SinglePromotionInfo temp = singlePromotionList.Find(x => x.PmId == singlePromotionInfo.PmId);
                                        if (temp == null)
                                        {
                                            temp = singlePromotionInfo;
                                            singlePromotionList.Add(temp);
                                        }

                                        if (temp.Stock < orderProductInfo.RealCount)
                                        {
                                            orderProductInfo.DiscountPrice = orderProductInfo.ShopPrice;
                                            orderProductInfo.PayCredits = 0;
                                            orderProductInfo.CouponTypeId = 0;
                                            orderProductInfo.ExtCode1 = -1 * orderProductInfo.ExtCode1;
                                            Carts.UpdateOrderProductSingle(new List<OrderProductInfo>() { orderProductInfo });
                                            throw new Exception("商品" + orderProductInfo.Name + "的单品促销活动库存不足,所以您当前只能享受普通购买，请返回购物车重新确认");
                                        }
                                        else
                                        {
                                            temp.Stock -= orderProductInfo.RealCount;
                                        }
                                    }
                                }

                                //验证赠品促销活动
                                if (orderProductInfo.ExtCode3 > 0)
                                {
                                    GiftPromotionInfo giftPromotionInfo = Promotions.GetGiftPromotionByPidAndTime(orderProductInfo.Pid, DateTime.Now);
                                    if (giftPromotionInfo == null)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品促销活动已经停止,请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.PmId != orderProductInfo.ExtCode3)
                                    {
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                    }
                                    else if (giftPromotionInfo.UserRankLower > partUserInfo.UserRid)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("你的用户等级太低，无法参加商品" + orderProductInfo.Name + "的赠品促销活动,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (giftPromotionInfo.QuotaUpper > 0 && orderProductInfo.BuyCount > giftPromotionInfo.QuotaUpper)
                                    {
                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                        Carts.DeleteOrderProductList(cartProductInfo.GiftList);
                                        throw new Exception("商品" + orderProductInfo.Name + "的赠品要求每单最多购买" + giftPromotionInfo.QuotaUpper + "个,所以您当前只能享受普通购买，请返回购物车重新确认");
                                    }
                                    else if (cartProductInfo.GiftList.Count > 0)
                                    {
                                        List<ExtGiftInfo> extGiftList = Promotions.GetExtGiftList(orderProductInfo.ExtCode3);
                                        if (extGiftList.Count != cartProductInfo.GiftList.Count)
                                        {
                                            throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                        }
                                        List<OrderProductInfo> newGiftOrderProductList = new List<OrderProductInfo>(extGiftList.Count);
                                        foreach (ExtGiftInfo extGiftInfo in extGiftList)
                                        {
                                            OrderProductInfo giftOrderProduct = Carts.BuildOrderProduct(extGiftInfo);
                                            Carts.SetGiftOrderProduct(giftOrderProduct, 1, orderProductInfo.RealCount, extGiftInfo.Number, giftPromotionInfo.PmId);
                                            newGiftOrderProductList.Add(giftOrderProduct);
                                        }

                                        //验证赠品信息是否改变
                                        for (int i = 0; i < newGiftOrderProductList.Count; i++)
                                        {
                                            OrderProductInfo newSuitOrderProductInfo = newGiftOrderProductList[i];
                                            OrderProductInfo oldSuitOrderProductInfo = cartProductInfo.GiftList[i];
                                            if (newSuitOrderProductInfo.Pid != oldSuitOrderProductInfo.Pid ||
                                                newSuitOrderProductInfo.Name != oldSuitOrderProductInfo.Name ||
                                                newSuitOrderProductInfo.ShopPrice != oldSuitOrderProductInfo.ShopPrice ||
                                                newSuitOrderProductInfo.MarketPrice != oldSuitOrderProductInfo.MarketPrice ||
                                                newSuitOrderProductInfo.CostPrice != oldSuitOrderProductInfo.CostPrice ||
                                                newSuitOrderProductInfo.Type != oldSuitOrderProductInfo.Type ||
                                                newSuitOrderProductInfo.RealCount != oldSuitOrderProductInfo.RealCount ||
                                                newSuitOrderProductInfo.BuyCount != oldSuitOrderProductInfo.BuyCount ||
                                                newSuitOrderProductInfo.ExtCode1 != oldSuitOrderProductInfo.ExtCode1 ||
                                                newSuitOrderProductInfo.ExtCode2 != oldSuitOrderProductInfo.ExtCode2 ||
                                                newSuitOrderProductInfo.ExtCode3 != oldSuitOrderProductInfo.ExtCode3 ||
                                                newSuitOrderProductInfo.ExtCode4 != oldSuitOrderProductInfo.ExtCode4 ||
                                                newSuitOrderProductInfo.ExtCode5 != oldSuitOrderProductInfo.ExtCode5)
                                            {
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品已经改变，请删除此商品后重新添加");
                                            }
                                        }

                                        //验证赠品库存
                                        foreach (OrderProductInfo gift in cartProductInfo.GiftList)
                                        {
                                            ProductStockInfo stockInfo = Products.GetProductStock(gift.Pid, productStockList);
                                            if (stockInfo.Number < gift.RealCount)
                                            {
                                                if (stockInfo.Number == 0)
                                                {
                                                    Carts.DeleteOrderProductList(new List<OrderProductInfo>() { gift });
                                                    if (cartProductInfo.GiftList.Count == 1)
                                                    {
                                                        orderProductInfo.ExtCode3 = -1 * orderProductInfo.ExtCode3;
                                                        Carts.UpdateOrderProductGift(new List<OrderProductInfo>() { orderProductInfo });
                                                    }
                                                }
                                                else
                                                {
                                                    gift.RealCount = stockInfo.Number;
                                                    Carts.UpdateOrderProductCount(new List<OrderProductInfo>() { gift });
                                                }
                                                throw new Exception("商品" + orderProductInfo.Name + "的赠品" + gift.Name + "库存不足,请返回购物车重新确认");
                                            }
                                            else
                                            {
                                                stockInfo.Number -= gift.RealCount;
                                            }
                                        }
                                    }
                                }

                                #endregion
                            }
                        }

                        #endregion
                    }
                    if (Carts.SumMallCartFullCut(storeCartList) != fullCut)
                        throw new Exception("满减金额不正确,请刷新页面重新提交");

                    //验证已经通过,进行订单保存
                    int pCount = 0;
                    string oidList = "";
                    foreach (StoreCartInfo storeCartInfo in storeCartList)
                    {
                        List<SinglePromotionInfo> storeSinglePromotionList = singlePromotionList.FindAll(x => x.StoreId == storeCartInfo.StoreInfo.StoreId);
                        List<CouponInfo> storeCouponList = couponList.FindAll(x => x.StoreId == storeCartInfo.StoreInfo.StoreId);
                        int storeFullCut = Carts.SumFullCut(storeCartInfo);

                        OrderInfo orderInfo = Orders.CreateOrder(partUserInfo, storeCartInfo.StoreInfo,
                            storeCartInfo.SelectedOrderProductList, storeSinglePromotionList,
                            fullShipAddressInfo, payMode == 0 ? 0 : 1, ref payCreditCount,
                            storeCouponList,
                            storeFullCut, buyerRemark, bestTime, WebHelper.GetIP(),
                            data.BookStartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                            data.BookEndTime.ToString("yyyy-MM-dd HH:mm:ss"), data.Sid);

                        if (orderInfo != null)
                        {
                            #region 计算余额，并更新付款金额、订单状态   2017-11-22 wh add

                            //计算余额
                            if (payCash > orderInfo.SurplusMoney)
                                payCash = orderInfo.SurplusMoney;   //在线付款额度 必须 >= 余额支付额度

                            decimal surplusMoney = orderInfo.SurplusMoney - payCash;
                            int orderState = orderInfo.OrderState;

                            if (surplusMoney <= 0)
                            {
                                orderState = (int)OrderState.PreProducting; //不需要调用微信支付,改为：备货中
                                                                            //更新付款金额,订单状态
                                int count = Manager.Manager.Order.OrderManager.UpdatePayCash(orderInfo.Oid, payCash, surplusMoney, orderState);
                                //余额扣款
                                if (count > 0)
                                    CashOrders.ConsumeCash(orderInfo.Uid, orderInfo.StoreId, payCash, "");
                            }
                            else
                                Manager.Manager.Order.OrderManager.UpdatePayCash(orderInfo.Oid, payCash, surplusMoney, orderState);

                            #endregion


                            //创建订单处理
                            OrderActions.CreateOrderAction(new OrderActionInfo()
                            {
                                Oid = orderInfo.Oid,
                                Uid = data.Uid,
                                RealName = Users.GetPartUserById(data.Uid)?.NickName,
                                ActionType = (int)OrderActionType.Submit,
                                ActionTime = DateTime.Now,
                                ActionDes = orderInfo.OrderState == (int)OrderState.WaitPaying ? "您提交了订单，等待您付款" : "您提交了订单，请等待系统确认"
                            });
                            //删除剩余的满赠赠品
                            if (storeCartInfo.RemainedOrderProductList.Count > 0)
                            {
                                List<OrderProductInfo> delOrderProductList = Carts.GetFullSendMinorOrderProductList(storeCartInfo.RemainedOrderProductList);
                                if (delOrderProductList.Count > 0)
                                {
                                    Carts.DeleteOrderProductList(delOrderProductList);
                                    pCount += Carts.SumOrderProductCount(storeCartInfo.RemainedOrderProductList) - delOrderProductList.Count;
                                }
                            }
                            oidList += orderInfo.Oid + ",";
                        }
                        else
                        {
                            throw new Exception("提交失败，请联系管理员");
                        }
                    }
                    //目前只支持单个店铺订单，所以这里应该只有一个订单号
                    return oidList.TrimEnd(',');

                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }

        }

        /// <summary>
        /// 创建当面付订单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public string SubmitDirectPaymentOrder(CreateDirectPaymentDto data)
        {
            try
            {
                lock (_directpaymentlocker)
                {

                    int payCreditCount = data.PayCreditCount;//支付积分
                    string[] couponIdList = StringHelper.SplitString(data.CouponIdList ?? "");//客户已经激活的优惠劵
                    string[] couponSNList = StringHelper.SplitString(data.CouponSNList ?? "");//客户还未激活的优惠劵

                    var partUserInfo = Users.GetPartUserById(data.Uid);
                    var creditConfig = StoreConfigManager.GetStoreCreditConfigInfo(partUserInfo.RegisterFromStoreId);
                    //验证支付积分
                    if (payCreditCount > 0)
                    {
                        if (payCreditCount > partUserInfo.PayCredits)
                            throw new Exception("你使用的" + (creditConfig?.PayCreditName ?? "") + "数超过你所拥有的" + partUserInfo.PayCredits + "数");
                    }
                    //验证优惠劵
                    List<CouponInfo> couponList = new List<CouponInfo>();
                    if (couponIdList.Length > 0)
                    {
                        foreach (string couponId in couponIdList)
                        {
                            int tempCouponId = TypeHelper.StringToInt(couponId);
                            if (tempCouponId > 0)
                            {
                                CouponInfo couponInfo = Coupons.GetCouponByCouponId(tempCouponId);
                                if (couponInfo == null)
                                    throw new Exception("优惠劵不存在");
                                else
                                    couponList.Add(couponInfo);
                            }
                        }
                    }
                    if (couponSNList.Length > 0)
                    {
                        foreach (string couponSN in couponSNList)
                        {
                            if (!string.IsNullOrWhiteSpace(couponSN))
                            {
                                CouponInfo couponInfo = Coupons.GetCouponByCouponSN(couponSN);
                                if (couponInfo == null)
                                    throw new Exception("优惠劵" + couponSN + "不存在");
                                else
                                    couponList.Add(couponInfo);
                            }
                        }
                    }
                    foreach (CouponInfo couponInfo in couponList)
                    {
                        #region  验证

                        CouponTypeInfo couponTypeInfo = Coupons.GetCouponTypeById(couponInfo.CouponTypeId);
                        if (couponTypeInfo == null)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵类型不存在");
                        }
                        else if (couponTypeInfo.State == 0)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵类型已关闭");
                        }
                        else if (couponTypeInfo.UseExpireTime == 0 && couponTypeInfo.UseStartTime > DateTime.Now)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵还未到使用时间");
                        }
                        else if (couponTypeInfo.UseExpireTime == 0 && couponTypeInfo.UseEndTime <= DateTime.Now)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵已过期");
                        }
                        else if (couponTypeInfo.UseExpireTime > 0 && couponInfo.ActivateTime <= DateTime.Now.AddDays(-1 * couponTypeInfo.UseExpireTime))
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵已过期");
                        }
                        else if (couponTypeInfo.UserRankLower > partUserInfo.UserRid)
                        {
                            throw new Exception("你的用户等级太低，不能使用编号为" + couponInfo.CouponSN + "优惠劵");
                        }
                        else if (couponList.Count > 1 && couponTypeInfo.UseMode == 1)
                        {
                            throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵不能叠加使用");
                        }
                        else
                        {

                            if (couponTypeInfo.OrderAmountLower > data.OrderAmount)
                            {
                                throw new Exception("订单金额太低，不能使用编号为" + couponInfo.CouponSN + "优惠劵");
                            }
                            if (couponTypeInfo.LimitProduct == 1)
                            {
                                throw new Exception("编号为" + couponInfo.CouponSN + "优惠劵只能在购买指定商品时使用");
                            }
                        }

                        #endregion
                    }

                    DirectPayment orderInfo = DirectPaymentService.CreateDirectPayment(data.Uid, data.StoreId, data.OrderAmount, data.PayCash, data.PayCreditCount, couponList, WebHelper.GetIP());

                    if (orderInfo != null && orderInfo.Pid > 0)
                    {
                        return orderInfo.Pid.ToString();
                    }
                    else
                    {
                        throw new Exception("提交失败，请联系管理员");
                    }

                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }

        }

        /// <summary>
        /// 获取用户的当面付订单
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="uid"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageNumber"></param>
        /// <returns></returns>
        public DirectPaymentListModel GetUserDirectPayList(int storeId, int uid, int pageSize = 15, int pageNumber = 1)
        {

            var result = DirectPaymentManager.GetStorePage($"storeid={storeId} and uid={uid}", pageNumber, pageSize);
            PageModel pageModel = new PageModel(pageSize, pageNumber, (int)result.TotalItems);
            DirectPaymentListModel model = new DirectPaymentListModel()
            {
                OrderList = result.Items.Select(t => new DirectPaymentModel
                {
                    AddTime = t.AddTime,
                    CouponMoney = t.CouponMoney,
                    OrderAmount = t.OrderAmount,
                    OrderState = t.OrderState,
                    PayCash = t.PayCash,
                    PayCreditCount = t.PayCreditCount,
                    PayCreditMoney = t.PayCreditMoney,
                    PaySN = t.PaySN,
                    PaySystemName = t.PaySystemName,
                    PayTime = t.PayTime,
                    Pid = t.Pid,
                    PSN = t.PSN,
                    StoreId = t.StoreId,
                    SurplusMoney = t.SurplusMoney,
                    User = Users.GetPartUserById(t.Uid)
                }).ToList(),
                PageModel = pageModel

            };
            return model;
        }
        /// <summary>
        /// 取消当面付订单
        /// </summary>
        /// <param name="pid">当面付订单ID</param>
        /// <returns></returns>
        public bool CancelDirectPayOrder(int pid)
        {
            var payment = DirectPaymentManager.GetPaymentByPid(pid);
            DirectPaymentManager.UpdateDirectPaymentStatus(pid, 2);//更新订单状态
            if (payment.CouponMoney > 0 && !string.IsNullOrEmpty(payment.CouponIds)) //退回用户使用的优惠劵
            {
                foreach (var s in payment.CouponIds.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                {
                    int id;
                    if (int.TryParse(s, out id))
                    {
                        DirectPaymentManager.ReturnCoupon(id);
                    }
                }
            }

            if (payment.PayCreditCount > 0)//退回用户使用的积分
            {
                var creditLogInfo = new CreditLogInfo
                {
                    Uid = payment.Uid,
                    PayCredits = payment.PayCreditCount,
                    RankCredits = 0,
                    Action = (int)CreditAction.ReturnOrderUse,
                    ActionCode = payment.Pid,
                    ActionTime = DateTime.Now,
                    ActionDes = "退回用户订单使用的积分:" + payment.PSN,
                    Operator = payment.Uid
                };
                Credits.SendPayCredits(creditLogInfo);
            }
            return true;
        }

        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="Uid">会员ID</param>
        /// <param name="orderState">订单状态(0全部,20 待付款,40 确认中,60 已确认,80 备货中,100 已发货,120 已收货,140 锁定,160 取消)</param>
        /// <param name="page">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        public UserOrders GetUserOrders(int Uid, int orderState = 0, int page = 1, int pageSize = 10)
        {
            var startAddTime = "";
            var endAddTime = "";

            var pageModel = new PageModel(pageSize, page, Orders.GetUserOrderCount(Uid, startAddTime, endAddTime, orderState));

            var dtList = Orders.GetUserOrderList(Uid, pageModel.PageSize, pageModel.PageNumber, startAddTime, endAddTime, orderState);
            var orderList = new List<OrderModel>();

            foreach (DataRow row in dtList.Rows)
            {
                var order = new OrderModel()
                {
                    AddTime = Convert.ToDateTime(row["AddTime"]),
                    Consignee = Convert.ToString(row["Consignee"]),
                    IsReview = Convert.ToInt32(row["IsReview"]),
                    Oid = Convert.ToInt32(row["Oid"]),
                    OrderAmount = Convert.ToDecimal(row["OrderAmount"]),
                    OrderState = Convert.ToInt32(row["OrderState"]),
                    OSN = Convert.ToString(row["OSN"]),
                    PayMode = Convert.ToInt32(row["PayMode"]),
                    StoreId = Convert.ToInt32(row["StoreId"]),
                    StoreName = Convert.ToString(row["StoreName"]),
                    SurplusMoney = Convert.ToDecimal(row["SurplusMoney"]),
                    Uid = Convert.ToInt32(row["Uid"]),
                    BookStartTime = Convert.ToDateTime(row["bookstarttime"]),
                    BookEndTime = Convert.ToDateTime(row["bookendtime"]),
                    Sid = row["sid"].ToString()
                };
                order.ProductInfos = Orders.GetOrderProductList(order.Oid);
                orderList.Add(order);
            }

            return new UserOrders() { PageModel = pageModel, OrderList = orderList };
        }

        /// <summary>
        /// 统计订单状态
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public OrderStatistics Statistics(int uid)
        {
            try
            {
                var noPay = Orders.GetUserOrderCount(uid, DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 20);
                var noTransfer = Orders.GetUserOrderCount(uid, DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 80);
                var noConfirm = Orders.GetUserOrderCount(uid, DateTime.Now.AddDays(-10).ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 100);
                var success = Orders.GetUserOrderCount(uid, DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 120);
                return new OrderStatistics() { count_id_no_pay = noPay, count_id_no_transfer = noTransfer, count_id_no_confirm = noConfirm, count_id_success = success };
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }


        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="uid">会员ID</param>
        /// <param name="oid">订单ID</param>
        /// <returns></returns>
        public bool CancelOrder(int uid, int oid)
        {
            try
            {
                var orderInfo = Orders.GetOrderByOid(oid);
                if (orderInfo == null || orderInfo.Uid != uid)
                    throw new Exception("订单不存在");

                if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                    throw new Exception("订单当前不能取消");

                var partUserInfo = Users.GetPartUserById(uid);
                //取消订单
                Orders.CancelOrder(ref partUserInfo, orderInfo, uid, DateTime.Now);
                //创建订单处理
                OrderActions.CreateOrderAction(new OrderActionInfo()
                {
                    Oid = oid,
                    Uid = uid,
                    RealName = Users.GetUserDetailById(uid).RealName,
                    ActionType = (int)OrderActionType.Cancel,
                    ActionTime = DateTime.Now,
                    ActionDes = "您取消了订单"
                });

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }


        }

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="uid">会员ID</param>
        /// <param name="oid">订单ID</param>
        /// <returns></returns>
        public bool CancelOrderForBook(int uid, int oid)
        {
            try
            {
                var orderInfo = Orders.GetOrderByOid(oid);
                if (orderInfo == null || orderInfo.Uid != uid)
                    throw new Exception("订单不存在");

                if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                    throw new Exception("订单当前不能取消");

                var partUserInfo = Users.GetPartUserById(uid);
                //取消订单
                Orders.CancelOrderForBook(ref partUserInfo, orderInfo, uid, DateTime.Now);
                //创建订单处理
                OrderActions.CreateOrderAction(new OrderActionInfo()
                {
                    Oid = oid,
                    Uid = uid,
                    RealName = partUserInfo.NickName,
                    ActionType = (int)OrderActionType.Cancel,
                    ActionTime = DateTime.Now,
                    ActionDes = "您取消了订单"
                });

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }


        }

        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="uid">会员ID</param>
        /// <param name="osn">订单号</param>
        /// <returns></returns>
        public OrderInfoModel GetOrderByOSN(int uid, string osn)
        {
            var orderInfo = Orders.GetOrderByOSN(osn);
            if (orderInfo == null || orderInfo.Uid != uid)
                throw new Exception("订单不存在");
            var actions = OrderActions.GetOrderActionList(orderInfo.Oid);
            actions.Reverse();
            var oPros = Orders.GetOrderProductList(orderInfo.Oid);
            var pList = oPros.Select(pro => new OrderProductDetailModel
            {
                Info = pro,
                SkuItemList = Products.GetProductSkuItemInfoList(pro.Pid)
            })
                .ToList();
            var model = new OrderInfoModel
            {
                OrderInfo = orderInfo,
                RegionInfo = Regions.GetRegionById(orderInfo.RegionId),
                OrderProductList = pList,
                OrderActionInfo = actions,
            };
            return model;
        }
        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="uid">会员ID</param>
        /// <param name="oid">订单号</param>
        /// <returns></returns>
        public OrderInfoModel GetOrder(int uid, int oid)
        {
            var orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null || orderInfo.Uid != uid)
                throw new Exception("订单不存在");
            var actions = OrderActions.GetOrderActionList(oid);
            actions.Reverse();
            var oPros = Orders.GetOrderProductList(oid);
            var pList = oPros.Select(pro => new OrderProductDetailModel
            {
                Info = pro,
                SkuItemList = Products.GetProductSkuItemInfoList(pro.Pid)
            })
                .ToList();

            //订单的付款方式: 如余额支付
            var payCash = Manager.Manager.Order.OrderManager.GetPayCash(oid);
            Dictionary<string, object> payInfo = new Dictionary<string, object>();
            payInfo.Add("PayCash", payCash);

            //显示餐桌号
            if (!string.IsNullOrEmpty(orderInfo.Sid))
            {
                var diningTable = DiningTableManager.GetDiningTable(TypeHelper.StringToInt(orderInfo.Sid));
                orderInfo.Sid = diningTable != null ? string.Concat(diningTable.Name, " ", diningTable.TableNumber) : string.Empty;
            }

            var model = new OrderInfoModel
            {
                OrderInfo = orderInfo,
                RegionInfo = Regions.GetRegionById(orderInfo.RegionId),
                OrderProductList = pList,
                OrderActionInfo = actions,
                PayInfo = payInfo
            };
            return model;
        }

        /// <summary>
        /// 收货
        /// </summary>
        /// <returns></returns>
        public bool ReceiveOrder(int uid, int oid)
        {


            OrderInfo orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null || orderInfo.Uid != uid)
                throw new Exception("订单不存在");

            if (orderInfo.OrderState != (int)OrderState.Sended)
                throw new Exception("订单当前不能收货");
            var partUserInfo = Users.GetPartUserById(uid);
            //收货
            Orders.ReceiveOrder(ref partUserInfo, orderInfo, DateTime.Now, WebHelper.GetIP());
            //创建订单处理
            OrderActions.CreateOrderAction(new OrderActionInfo()
            {
                Oid = oid,
                Uid = uid,
                RealName = partUserInfo.NickName,
                ActionType = (int)OrderActionType.Receive,
                ActionTime = DateTime.Now,
                ActionDes = "您已经收货"
            });
            return true;
        }
        /// <summary>
        /// 获得有效的优惠劵列表
        /// </summary>
        /// <param name="selList"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<ValidCouponModel> GetValidCouponList(string selList, int uid)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);
                var partUserInfo = Users.GetPartUserById(uid);
                var result = new List<ValidCouponModel>();
                var rows = Coupons.GetUnUsedCouponList(uid).Rows;
                foreach (DataRow row in rows)
                {
                    if (TypeHelper.ObjectToInt(row["state"]) == 0)
                    {
                        continue;
                    }
                    if (TypeHelper.ObjectToInt(row["useexpiretime"]) == 0 && TypeHelper.ObjectToDateTime(row["usestarttime"]) > DateTime.Now)
                    {
                        continue;
                    }
                    if (TypeHelper.ObjectToInt(row["useexpiretime"]) == 0 && TypeHelper.ObjectToDateTime(row["useendtime"]) <= DateTime.Now)
                    {
                        continue;
                    }
                    if (TypeHelper.ObjectToInt(row["useexpiretime"]) > 0 && TypeHelper.ObjectToDateTime(row["activatetime"]) <= DateTime.Now.AddDays(-1 * TypeHelper.ObjectToInt(row["useexpiretime"])))
                    {
                        continue;
                    }
                    if (TypeHelper.ObjectToInt(row["userranklower"]) > partUserInfo.UserRid)
                    {
                        continue;
                    }
                    int storeId = TypeHelper.ObjectToInt(row["storeid"]);
                    StoreCartInfo storeCartInfo = storeCartList.Find(x => x.StoreInfo.StoreId == storeId);
                    if (storeCartInfo == null)
                    {
                        continue;
                    }
                    if (TypeHelper.ObjectToInt(row["orderamountlower"]) > Carts.SumOrderProductAmount(storeCartInfo.SelectedOrderProductList))
                    {
                        continue;
                    }
                    int limitStoreCid = TypeHelper.ObjectToInt(row["limitstorecid"]);
                    if (limitStoreCid > 0)
                    {
                        bool hasCid = false;
                        foreach (OrderProductInfo orderProductInfo in storeCartInfo.SelectedOrderProductList)
                        {
                            if (orderProductInfo.Type == 0 && orderProductInfo.StoreCid == limitStoreCid)
                            {
                                hasCid = true;
                            }
                        }
                        if (!hasCid) continue;
                    }
                    if (TypeHelper.ObjectToInt(row["limitproduct"]) == 1)
                    {
                        List<OrderProductInfo> commonOrderProductList = Carts.GetCommonOrderProductList(storeCartInfo.SelectedOrderProductList);
                        if (!Coupons.IsSameCouponType(TypeHelper.ObjectToInt(row["coupontypeid"]), Carts.GetPidList(commonOrderProductList)))
                        {
                            continue;
                        }
                    }
                    result.Add(new ValidCouponModel
                    {
                        CouponId = Convert.ToInt32(row["couponid"]),
                        Money = Convert.ToInt32(row["money"]),
                        Name = row["name"].ToString(),
                        UseMode = Convert.ToInt32(row["usemode"])

                    });
                }
                return result.OrderByDescending(t => t.Money).ToList();
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }


        /// <summary>
        /// 手动发放佣金，针对订单漏发佣金
        /// </summary>
        /// <param name="osn">订单编号</param>
        /// <returns></returns>
        public bool CaculateBrokerage(string osn)
        {
            var orderInfo = Orders.GetOrderByOSN(osn.Trim());
            if (orderInfo != null && orderInfo.OrderState >= 100)
            {
                Distributors.CaculateBrokerage(orderInfo);
                return true;
            }
            return false;


        }

        /// <summary>
        /// 查询订单物流进度
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public object GetOrderLogistics(int oid, int isShowImg = 1)
        {

            var orderInfo = Orders.GetOrderByOid(oid);
            if (orderInfo == null)
                throw new Exception("订单不存在");

            if (orderInfo.OrderState != (int)OrderState.Sended)
                throw new Exception("只能查询发货中的订单");


            //支持的物流查询公司
            var shipper = KDNiaoSdk.GetShipperList().FirstOrDefault(x => (x.Name.Contains(orderInfo.ShipCoName) || orderInfo.ShipCoName.Contains(x.Name)));
            if (shipper == null)
                throw new Exception($"不支持查询{orderInfo.ShipCoName}的订单");

            var param = new TrancesParam() { LogisticCode = orderInfo.ShipSN, ShipperCode = shipper.Code };
            var jsonModel = KDNiaoSdk.GetTraces(param);

            //显示错误消息
            if (!KDNiaoSdk.IsSuccess(jsonModel))
                throw new Exception(jsonModel.Reason);

            //订单商品列表
            var productList = isShowImg == 1 ? Orders.GetOrderProductList(oid) : null;

            var model = new
            {
                ShowImg = productList != null ? productList[0].ShowImg : "",
                ShipCoName = shipper.Name,
                ShipSN = orderInfo.ShipSN,
                Phone = "",
                Traces = jsonModel.Traces
            };

            return model;
        }
    }
}
